问题
I just found DataMapper for CI's built in to_json
and all_to_json
methods, which saved me a ton of time.
How can I return relations with the result?
Currently my code looks like this:
$homes = new Home();
$homes->include_related('address')->get();
$homes->include_related('album')->get();
$homes->get();
$homes->set_json_content_type();
echo $homes->to_json();
However I'm only getting back the home itself, rather than the nested information.
EDIT:
I've found that you can simply add those fields to the array parameter, however, if I use all_to_json($fields);
it returns the first row perfectly, but the second nested comes back as NULL. Still think it's my fault, but I didn't know if anyone's seen this before.
Here is my current output.
{
"id": 1,
"latitude": "0",
"longitude": "0",
"price": "173000",
"sqft": "2100",
"room_count": "3",
"bath_count": "2",
"created": "1333209034",
"updated": "1333209034",
"status": "active",
"address": [
1
],
"album": []
}
In my data, if I view the raw output, I see the full address. In my JSON I see a number 1. How can I display this data rather than the number?
回答1:
The 1.8.2. release version doesn't recurse, to avoid generating gigantic JSON ouput (depending on the loaded object this can run out of memory very quickly).
There are some updates available to the JSON extension on bitbucket as part of the upcoming 1.8.3 release.
On of the new features is that you can pass it a list of related objects it has to recurse into, in deep relationship notation (like "child", "child/grandchild", "child/other", "child/other/related", ...).
来源:https://stackoverflow.com/questions/10593008/datamapper-all-to-json-method-returning-slashes