I'm back ...
The example shows how to work with only one results, but I need to encode to JSON an array of results.
Is there another way?
Thanks
Currently, you'd just have to iterate over the results and call to_json() on each. Not really ideal but I'll be addressing this in a future version.
Hi,
Sometimes you'll need to have double quotes for each values of the JSON string (for example for using jQuery EasyUI DataGrid). Here's a way to do this :
$result = array();
$persons = Person::find('all');
foreach ($persons as $p) {
array_push($result, $p->to_json());
}
$result = "[".implode(",", $result)."]";
$result = str_replace('":', '":"', $result);
$result = str_replace(',"', '","', $result);
$result = str_replace('""', '"', $result);
+1 for toJson on results iterator!
I found a good way to achieve this :
$result = array();
$persons = Person::all();
foreach($persons as $person) {
array_push($result, $person->to_array()); //using to_array instead of to_json
}
echo json_encode($result);
Your solution didn't work for me. I have a Fatal error with message 'Call to undefined method: to_array'.
What version of PHP ActiveRecord do you use?
The README shows 1.0, butI think it's the Nightly Build, cause I get the lib with Composer, and set the version to "dev-master". Take a look at my composer.json file:
{
"require" : {
"slim/slim" : "1.6.*",
"php-activerecord/php-activerecord" : "dev-master"
}
}
(1-8/8)
Subject: JSON encoding
Hi all,
I need to create a JSON encoded string of the results of a query, is there an example on how to do this?
Thanks to all