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

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

In Ruby, I believe this is possible, perhaps with the help of the .map() method. (I'm not a guy from Ruby ...)

What I want is a simple method call, for example toArray() in Zend_DB_Table, and not foreach or something like that, but I can not find it in docs .

In PHP ActiveRecord, getting the result is very simple:

 $settings = SystemSettings::all(); 

But it returns 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] => ) 

Although in many cases this is really great, I would just like to have a simple array, for example:

 Array ( [author] => Hawle [base_url] => example.com ) 
+6
source share
8 answers

I had a similar problem, hope this can help someone who came across 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(); } } 

Then I can call β†’ to_array () and return the array.

Hope this helps!

+7
source

I was looking for the answer to this question to create an array of results that can be easily json-encoded and sent as an answer to an ajax call. I wanted only the attributes of each object in the result array.

Unfortunately, you cannot just call to_json () for each result, and then json-encode all this; you get double coding. Fortunately, the function and class published by @willwashburn to solve this problem are now included in php-activerecord, although they do not seem to fall 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. Then you can do something like

 echo(json_encode($data)); 

if you want to send it as an answer to an ajax call.

+4
source
 class MyPHPActiveRecord extends PHPActiveRecord { public function toJSON() { return json_encode(get_object_vars($this)); } } 
+1
source

This is my decision:

 $posts = Post::find('all'); $arrayResult = array_map(function($res){ return $res->attributes(); }, $posts); printf('<pre>%s</pre>', print_r($arrayResult, true)); 
+1
source

You can do it as follows:

 funciton ar2array($settings){ $arr = array(); foreach($settings as $fieldObj){ $fieldName = $fieldObj->attributes["param"]; $fieldValue = $fieldObj->attributes["value"]; $arr[$fieldName] = $fieldValue; } return $arr; } $resultAsYouWant = ar2array($settings); 

Hope this helps. Greetings

PS: If ->attributes is private, then its accesor method (must be one) as ->getAttributes() or equivalent.

0
source

I found myself looking for a solution to the same problem that I encountered using the Yii scheme - in Yii there is an easier way to do this.

 $posts = Posts::model()->findAll(); foreach($posts as $result) { print_r($result->attributes); } 

It prints a simple array on request:

 Array ( [id] => 1 [title] => Title [text] => Text ) 

Hope this helps someone.

0
source

My decision:

  • The following method is added for the utils class found in lib \ Utils.php

     public static function results_to_json($data) { $arr = array(); if(count($data)>0){ foreach($data as $row){ array_push($arr, $row->to_array()); } } return json_encode($arr); } 
  • Call:

     echo \ActiveRecord\Utils::results_to_json($dataobject); 
0
source

Obviously, this no longer applies to OP; however, given that it still took me over an hour to find a solution for this (without the help of php-activerecords docs), this might help someone else.

  $r = Model::find('$id')->attributes(); $a = []; foreach ($r as $k => $v) { $a[$k] = $v; } 

Perhaps not the most elegant, but works great.

0
source

Source: https://habr.com/ru/post/887484/


All Articles