Bug Report #2860

Database_Result::as_array inconsistent with ORM::as_array

Added by Sahand S about 4 years ago. Updated almost 4 years ago.

Status:ClosedStart date:05/11/2010
Priority:NormalDue date:
Assignee:Woody Gilk% Done:

0%

Category:Modules:Database
Target version:v3.0.8
Resolution:invalid Points:

Description

Database_Result::as_array returns an array of ORM objects, instead of an array of key=>value arrays representing the objects, as one would expect. The following code demonstrates the issue:

        $role = new Model_Role(2);        
        echo Kohana::debug($role->as_array()); // array(3) ( "id" => string(1) "2" ,    "name" => string(5) "admin",    etc...) as expected

        $roles = ORM::factory('role')->find_all();
        echo Kohana::debug($roles->as_array()); 
        /* Outputs:         
            array(1) (
            0 => object Model_Role(35) {
                protected _db => object Database_MySQL(5) {
                    protected _identifier => string(1) "`" 
                    public last_query => string(55) "SELECT `roles`.* FROM `roles` ORDER BY `roles`.`id` ASC" 
                    protected _instance => string(9) "vservices" 
                    protected _connection => resource(mysql link)
                    protected _config => array(5) (
                        "type" => string(5) "mysql" 
                        "table_prefix" => string(0) "" 
                        "charset" => string(4) "utf8" 
                        "caching" => bool FALSE
                        "profiling" => bool TRUE
                    )
                }
                etc...
        */

        // So, for example:
        echo json_encode($roles->as_array()); // This will not produce the desired output 

        // How roles->as_array should be to be consistent with ORM::as_array
        $roles_as_array = array();
        foreach($roles as $r){
            $roles_as_array[] = $r->as_array();
        }

        echo Kohana::debug($roles_as_array);

        // Now I can do something like
        echo json_encode($roles_as_array);            

I propose the following very simple change be implemented:

File system/modules/database/classes/kohana/database/result.php

Change from

94                // Add each row to the array
95                $results[] = $row;

To:

94                // Add each row to the array
95                $results[] = $row->as_array();

Related issues

Related to Kohana v3.x - Feature Request #3024: A method to convert Database_Result object to an array of... Closed 06/28/2010

History

#1 Updated by Woody Gilk about 4 years ago

  • Assignee set to John Heathco

#2 Updated by Woody Gilk about 4 years ago

  • Target version set to v3.0.6

#3 Updated by Woody Gilk about 4 years ago

  • Target version changed from v3.0.6 to v3.0.7

#4 Updated by John Heathco about 4 years ago

  • Category changed from Modules:ORM to Modules:Database
  • Assignee deleted (John Heathco)

#5 Updated by Woody Gilk about 4 years ago

  • Target version changed from v3.0.7 to v3.0.8

#6 Updated by Woody Gilk almost 4 years ago

  • Status changed from New to Closed
  • Assignee set to Woody Gilk
  • Resolution set to invalid

The as_array method does not imply that the results will be an array of arrays, but an array (as opposed to iterator) of the results.

Also available in: Atom PDF