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

#1
[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();
$c->set_json_content_type();
echo $c->to_json(array('id', 'Make'), TRUE);

output

Code:
{
  "id": 14,
  "Make": "Astra"
}

any ideas?

#2
[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
   - id
   - address_id
   - etc...
addresses
   - id
   - address1
   - city
   - state
   - etc...
features
   - id
   - name
homes_features
   - 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.

Code:
$homes = new Home();

$homes->get();
$homes->address->get();
$homes->feature->get();

$homes->set_json_content_type();

$home_fields = array(
'id',
'latitude',
'longitude',
'price',
'sqft',
'room_count',
'bath_count',
'created',
'updated',
'status',
'address',
'feature'
);

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

However this outputs the following JSON.

Code:
{
    "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": [
        1
    ],
    "feature": [
        1,
        2,
        3,
        4,
        5
    ]
}

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

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

#3
[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.

#4
[eluser]seth.aldridge[/eluser]
Thanks for the response. I'm assuming that I'll need to do it manually using
Code:
all_to_array()
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.

#5
[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.

#6
[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)
{
$rels[] = $item->id;
}
$result[$f] = $rels;

To something like

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

#7
[eluser]WanWizard[/eluser]
at your service: https://bitbucket.org/wanwizard/datamapp...33f454c7b5 (untested!)

#8
[eluser]seth.aldridge[/eluser]
Thank you for knocking this out. I'll click that donate button in the docs. Smile

#9
[eluser]WanWizard[/eluser]
[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

#10
[eluser]seth.aldridge[/eluser]
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.