How do I json_encode a Kohana ORM Result as an array
I’m working on an Kohana based API where I needed to return some data from an ORM result as an array. Sure, I can loop through the ORMIterator and assign the results to an array, but there must be a nicer way of doing this. I ran across this thread which uses and anonymous function to do the as_array() call. See:
echo json_encode( array_map( create_function( '$obj', 'return $obj->as_array();', ORM::factory('image')->find_all()->as_array() ) );
Global config variable in Kohana
I’ve been recently on a project that is using Kohana PHP framework. I had no knowledge of it coming in but a framework is a framework.
It is pretty easy to get a grasp on especially if you are used to Object-Oriented-Programming and Model-View-Controller setups.
One hiccup I ran into was trying to get a global variable like a config variable for instance inside the controller. The documentation is lacking this answer and since Kohana isn’t as widely used as Cakephp or Zend there wasn’t much in the way of Stack Overflow type answers from Google either.
If you’ve looked inside the Kohana config folder you can see that you can create and extend config files. I really liked this where I can create a new file in the config folder for all my email settings and name the file email.php. Seems much more organized. Now inside that file you set all your settings to the $config array.
$config['app_email'] = 'email@example.com';
That’s all pretty straight forward. But getting this variable inside a controller is a little different than what you would expect.
That looks pretty unorthodox once you’ve worked with other frameworks. Inside the static config() function, the parameter given needs a structure like:
I did finally find this answer on the web and still had to do some guessing to get it to work but hopefully this will help kohana users find this info sooner then I did.