Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a PHPActiveRecord result as simple array, not array of objects

I would like to have a simple a method, that can give back PHP Activerecord results as simple/associative arrays, not an array of ActiveRecord Objects.

In Ruby I believe this is done perhaps with .map() method. (I am not a Ruby guy...)

What I want is a simple method call, like toArray() in Zend_DB_Table, not a foreach, or something like that, but I can't seem to find it in their docs.

In PHP ActiveRecord getting a result is really easy:

$settings = SystemSettings::all();

But it gives back something like this:

[0] => SystemSettings Object
    (
        [errors] => 
        [attributes:ActiveRecord\Model:private] => Array
            (
                [param] => author
                [value] => Hawle
            )

        [__dirty:ActiveRecord\Model:private] => Array
            (
            )

        [__readonly:ActiveRecord\Model:private] => 
        [__relationships:ActiveRecord\Model:private] => Array
            (
            )

        [__new_record:ActiveRecord\Model:private] => 
    )

[1] => SystemSettings Object
    (
        [errors] => 
        [attributes:ActiveRecord\Model:private] => Array
            (
                [param] => base_url
                [value] => example.com
            )

        [__dirty:ActiveRecord\Model:private] => Array
            (
            )

        [__readonly:ActiveRecord\Model:private] => 
        [__relationships:ActiveRecord\Model:private] => Array
            (
            )

        [__new_record:ActiveRecord\Model:private] => 
    )

While this is really great in many cases, here, I would just like to have a simple array, like this:

Array
    (
        [author] => Hawle
        [base_url] => example.com
    )
like image 680
Gergely Havlicsek Avatar asked May 06 '11 21:05

Gergely Havlicsek


4 Answers

I had a similar issue hopefully this can help someone else who stumbles on it. Obviously, this is specific to phpactiverecord.org.

In /lib/Model.php I added the following function:

public function to_array(array $options=array())
{
    return $this->serialize('array', $options);
}

In /lib/Serialization.php I added the following class

class arraySerializer extends Serialization
{
    public static $include_root = false;

    public function to_s()
    {
        return self::$include_root ? array(strtolower(get_class($this->model)) => $this->to_a()) : $this->to_a();
    }


}

I can then call ->to_array() and get an array back.

Hope this helps!

like image 69
willwashburn Avatar answered Oct 13 '22 01:10

willwashburn


I was searching for the answer to this question in order to produce an array of results that could be easily json-encoded and sent as the response to an ajax call. I wanted only the attributes of each object in the array of results.

Unfortunately, you can't just call to_json() on each result and then json-encode the entire thing; you end up with double-encoding. Fortunately, though, the function and class posted by @willwashburn to solve this problem have now been included in php-activerecord, though they don't seem to have made it into the online documentation.

To return an array of results, do the following:

$data = MyModel::find('all');
foreach ($data as &$result) {
    $result = $result->to_array();
}

Your entire result set will now be an array of arrays, containing only the attributes of each object. You can then do something like

echo(json_encode($data));

if you want to send it as the response to an ajax call.

like image 28
Chris Wade Avatar answered Oct 13 '22 01:10

Chris Wade


This is my solution:

$posts = Post::find('all');

$arrayResult = array_map(function($res){
  return $res->attributes();
}, $posts);
printf('<pre>%s</pre>', print_r($arrayResult, true));
like image 22
RodolfoSilva Avatar answered Oct 13 '22 00:10

RodolfoSilva


class MyPHPActiveRecord extends PHPActiveRecord {

    public function toJSON() {
        return json_encode(get_object_vars($this));
    }
}
like image 33
dqhendricks Avatar answered Oct 13 '22 01:10

dqhendricks