Datamapper to_json question |
[eluser]fireport[/eluser]
Im trying to get results from a query formatted as a json result. I am only able to generate 1 result when there are 10 records in the table controller Code: $data['cars'] = $c->get(); output Code: { any ideas?
[eluser]seth.aldridge[/eluser]
Hi, Using this method, how do I return related and many-to-many results? I have a pretty straight forward DB setup. Code: homes When I use the to_json method I don't get back the address or features. Here is my controller. Code: $homes = new Home(); However this outputs the following JSON. Code: { These are the ID's of the feature and address. How can I pull back the fields so it looks like this. Code: "address": {
[eluser]WanWizard[/eluser]
'to_json()', like most other Datamapper method, operate on the current object, so that is what you get returned. If you want to convert the entire result set, use 'all_to_json()' instead. Note that this has some bugs fixed that are not part of the 1.8.2 distribution, so you might want to fetch the latest source from bitbucket.
[eluser]seth.aldridge[/eluser]
Thanks for the response. I'm assuming that I'll need to do it manually using Code: all_to_array() I grabbed the BitBucket version and that fixed the \ issue in the JSON, but unfortunately doesn't offer returning a complex JSON object. Pretty close, just not perfect. Thanks for building such a sweet addition to CI.
[eluser]WanWizard[/eluser]
Currently, to prevent generating enormous json structures, to_json() and all_to_json() only return the id's of the related objects. It isn't to difficult to add a parameter that allows you to dump the entire related structure recusively. I'll add it to the todo list.
[eluser]seth.aldridge[/eluser]
Looking at the code, it seems all I'd need to do is remove the specific call to just the ID. Code: foreach($object->{$f} as $item) To something like Code: foreach($object->{$f} as $item)
[eluser]WanWizard[/eluser]
at your service: https://bitbucket.org/wanwizard/datamapp...33f454c7b5 (untested!)
[eluser]seth.aldridge[/eluser]
Thank you for knocking this out. I'll click that donate button in the docs.
[eluser]WanWizard[/eluser]
[quote author="seth.aldridge" date="1337107546"]Thank you for knocking this out. I'll click that donate button in the docs. [/quote] Thanks. But please test it first...
|
Welcome Guest, Not a member yet? Register Sign In |