• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Datamapper to_json question

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


$data['cars'] = $c->get();
echo $c->to_json(array('id', 'Make'), TRUE);


  "id": 14,
  "Make": "Astra"

any ideas?


Using this method, how do I return related and many-to-many results?

I have a pretty straight forward DB setup.

   - id
   - address_id
   - etc...
   - id
   - address1
   - city
   - state
   - etc...
   - id
   - name
   - id
   - home_id
   - feature_id

When I use the to_json method I don't get back the address or features.

Here is my controller.

$homes = new Home();



$home_fields = array(

echo $homes->to_json($home_fields, true);

However this outputs the following JSON.

    "id": 1,
    "latitude": "35.38451037447432",
    "longitude": "-80.57186365127563",
    "price": "173000",
    "sqft": "2100",
    "room_count": "3",
    "bath_count": "2",
    "created": "1333209034",
    "updated": "1333209034",
    "status": "active",
    "address": [
    "feature": [

These are the ID's of the feature and address. How can I pull back the fields so it looks like this.

"address": {
    "address1": "1000 Somewhere Ln",
    "city": "Rural"
    "state": "Example",
"feature": [
        "id": 1,
        "name": "Large Bathroom"
        "id": 2,
        "name": "Updated Kitchen"

'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.

Thanks for the response. I'm assuming that I'll need to do it manually using
on all related db objects. Then merge those objects together and converting it to JSON.

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.

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.

Looking at the code, it seems all I'd need to do is remove the specific call to just the ID.

foreach($object->{$f} as $item)
$rels[] = $item->id;
$result[$f] = $rels;

To something like

foreach($object->{$f} as $item)
$rels[] = $item;
$result[$f] = $rels;

at your service: https://bitbucket.org/wanwizard/datamapp...33f454c7b5 (untested!)

Thank you for knocking this out. I'll click that donate button in the docs. Smile

[quote author="seth.aldridge" date="1337107546"]Thank you for knocking this out. I'll click that donate button in the docs. Smile[/quote]
Thanks. But please test it first... Wink

I'll test it once my kid goes to sleep. Smile

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.