Ramon C Mon May 21 07:47:07 -0400 2012

Subject: Json models

First, congratulations for the great job !
Im working with Json, and i dont know how can i do this:

$users = User::find('all');
$users->to_json();

Really thanks


Dell McKinney Thu May 31 10:40:32 -0400 2012

$users = User::find('all');
echo $arToJson($users);
or
echo $arToJson($users, array('exept' => 'password'));

function arToJson($data, $options = null) {
$out = "[";
foreach( $data as $row) {
if ($options != null)
$out .= $row->to_json($options);
else
$out .= $row->to_json();
$out .= ",";
}
$out = rtrim($out, ',');
$out .= "]";
return $out;
}

Ramon C Mon Jun 04 11:00:07 -0400 2012

Dell thanks,
really really useful !!
I thougth i was native in the framework .
R.

Roman Che Mon Dec 02 13:40:55 -0500 2013

Does it call getter during serialization? I got unserialized value :(

Joe Mamma Fri May 16 13:54:45 -0400 2014

Add method to lib\Utils.php in the Utils class

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

//retrieve data
$data->Model::All();

//output json
echo \ActiveRecord::Uils->results_to_json($dataobject)

(1-5/5)