4

I'm executing a simple query and want to get an array back. Based on the ORM tutorial that comes with the Kohana 3 guide, I thought I could do the following:

ORM::factory('user')->find_all()->as_array();

But that seems to give me an array of model objects (ie array( User_Model1, User_Model2 ...

Looking at the source I see I can easily fix this by hacking with the following patch.

modules/database/classes/kohana/database/result.php
@@ -94,7 +94,7 @@
                        foreach ($this as $row)
                        {
-                               $results[] = $row;
+                               $results[] = $row->as_array();

Which seems to be more in line with what the user guide says:

A powerful feature of ORM is the ORM::as_array method which will return the given record as an array. If used with ORM::find_all, an array of all records will be returned. A good example of when this is useful is for a select list:

// Display a select field of usernames (using the id as values) echo Form::select('user', ORM::factory('user')->find_all()->as_array('id', 'username'));

Wondering if this is intentional, if so, why? What would be a better work around if I do want to create an array of associative arrays?

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
Chris J
  • 131
  • 2
  • 2
  • 4
  • 2
    The point of having an orm is to provide an object interface (model), so you won't see the raw data (array). The `as_array()` method is coded as intended. – J-16 SDiZ Nov 01 '10 at 05:49
  • 1
    Ok. I thought I thought I may have misunderstood something like that. Forgive my misunderstanding then, but why does it have the as_array function? Couldn't anyone just use the Iterator interface to access the objects if they're not meant to be exposed as anything but Objects? – Chris J Dec 17 '10 at 03:40
  • The documentation is correct on this point, please note that they are passing arguments to the `is_array()` function. Doing so will result in an array with the first argument as key, and the second argument as value. – Thorsten Sep 14 '11 at 08:59

3 Answers3

5

This is intentional behaviour, as (clearly?) visible in the documentation, so please do not apply that "patch". Especially because you want to modify ORM (not only) itself.

Instead read this:

  • if as_array() is applied on the collection of rows, it returns array of rows (each row being separate object, not array),
  • if applied on single row, it returns the row as an array,

So you have at least two solutions:

  • transform each row explicitly after transforming collection to array,
  • add your own methon in the proxy class instead of changing the module code (Kohana has empty classes that inherit from core classes and that you can override by placing them in application/classes).

This specific class is called Kohana_Database_Result, so place class Database_Result in application/class/database/result.php and make it inherit from Kohana_Database_Result, then change what you need (if you need to change that).

Tadeck
  • 132,510
  • 28
  • 152
  • 198
0

You are using the ORM but it seems you don't really need its features (which is, among others, to return database rows as Model objects). In that case, a solution could be to use the Database class directly. From your model, you can do:

$output = $this->_db->query(Database::SELECT, "select * from users");

By default Database::query() will return an associative array.

laurent
  • 88,262
  • 77
  • 290
  • 428
-1

ORM has no methods like you describe. There was a select_list() method in Kohana 2.3.4, may be userguide contains wrong information from old (or future) version?

PS. And there is no reason for changing DB Result object, because $row may be any (not only ORM) class.

biakaveron
  • 5,493
  • 1
  • 16
  • 20
  • biakaveron, I'm not sure what you mean when you say ORM has no methods like I describe. Both the ORM and Database_Result classes have an as_array() function. (Looking at kohana 3.0.8) – Chris J Nov 01 '10 at 00:50
  • 1
    I mean the `select_list($key, $value)` functionality. `DB_Query::as_array()` only converts Iterator to standard array object. Al items will be the same (ORM for example). – biakaveron Nov 01 '10 at 07:21