Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

as_array for multiple results #17

Closed
j4mie opened this issue Jan 28, 2011 · 9 comments
Closed

as_array for multiple results #17

j4mie opened this issue Jan 28, 2011 · 9 comments
Assignees
Milestone

Comments

@j4mie
Copy link
Owner

j4mie commented Jan 28, 2011

From Osman Üngür (by email):

Is as_array() can be usable with find_many() for array output ? (I tested, as_array() with
find_many() gives me an error)
(Its simply possible with iterating result object to Redis Lists but i want to set json encoded
array output to Redis key) (I dont want to iterate it again and again)

@j4mie
Copy link
Owner Author

j4mie commented Jan 28, 2011

Idea: Make as_array() work on unexecuted queries. So, you could call
ORM::for_table('table')->as_array();

@MarcosT
Copy link

MarcosT commented Sep 13, 2011

I solve the problem this (into idiorm.php):

    public static function to_array( $arr_orm ) {
        $salida = array();
        
        if( is_array( $arr_orm ) ) {
            $size = sizeof( $arr_orm );
        
            for( $i = 0; $i < $size; ++$i ) {
                $obj = $arr_orm[$i];

                if ( $obj instanceof ORM ) {
                    $salida[$i] = $arr_orm[$i]->as_array();
                }
            } 
        }
        
        return $salida;
    }

And you can use this way:

$tnomina = ORM::for_table('nomina.tnomina')->find_many(); ORM::to_array( $tnomina )

@pulunomoe
Copy link

Hello, here's my correction for the above solution :

public static function to_array($result) {
    $array = array();
    foreach ($result as $r) {
        $array[] = (array) $r->_data;
    }
    return $array;
}

@Surt
Copy link
Contributor

Surt commented Dec 15, 2011

You don't need that code, use this one instead... less resources consuming since _run it's returning the array you are looking for:

    public function find_array() {
        return $this->_run(); 
    }

$tnomina = ORM::for_table('nomina.tnomina')->find_array();

@codecowboy
Copy link

@Surt Where should this code go? If I try accessing the properties of my orm objects in my application, I just get null values, presumably because the properties are protected?

@Surt
Copy link
Contributor

Surt commented Mar 12, 2012

It goes directly before, or after find_many or find_one, as another method.
since
[code]
return $this->_run();
[/code]
returns an array, you will end with a raw array:

$tnomina = ORM::for_table('nomina.tnomina')->find_array();
var_dump($tnomina); // will return

array(
0 => array(....a row),
1 => array(...another row),
.
.
);

@codecowboy
Copy link

@Surt - thanks. Works great. Perhaps there should be a PR for this?

@neochar
Copy link

neochar commented Apr 4, 2012

@Surt, man it's great) thanks a lot.

@treffynnon
Copy link
Collaborator

I like @Surt approach to this problem so adding a find_array method for 1.2.0. Closed in commit 3c082ca

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

7 participants