Datamapper to_json question - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Datamapper to_json question (/showthread.php?tid=48461) Pages:
1
2
|
Datamapper to_json question - El Forum - 01-17-2012 [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? Datamapper to_json question - El Forum - 05-14-2012 [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": { Datamapper to_json question - El Forum - 05-15-2012 [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. Datamapper to_json question - El Forum - 05-15-2012 [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. Datamapper to_json question - El Forum - 05-15-2012 [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. Datamapper to_json question - El Forum - 05-15-2012 [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) Datamapper to_json question - El Forum - 05-15-2012 [eluser]WanWizard[/eluser] at your service: https://bitbucket.org/wanwizard/datamapper/changeset/fe33f454c7b5 (untested!) Datamapper to_json question - El Forum - 05-15-2012 [eluser]seth.aldridge[/eluser] Thank you for knocking this out. I'll click that donate button in the docs. Datamapper to_json question - El Forum - 05-15-2012 [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... Datamapper to_json question - El Forum - 05-15-2012 [eluser]seth.aldridge[/eluser] I'll test it once my kid goes to sleep. |