Welcome Guest, Not a member yet? Register   Sign In
Datamapper to_json question
#11

[eluser]seth.aldridge[/eluser]
Money well spent. Worked like a champ.

However, all_to_json() doesn't return the nested items.

After looking into the above mentioned issue, it appears to be that the all_to_json isn't making it into the foreach loop.
#12

[eluser]WanWizard[/eluser]
Hmm... That's odd.

All all_to_json() does is foreach() over the object instead of only converting the current object. It calls to_json() on each of the objects, and that's where the magic happens.

Guess I'll have to do some testing after all. It'll be tomorrow, it's way past my bed time at the moment. Wink
#13

[eluser]seth.aldridge[/eluser]
Not to worry. For what I needed it works very well. I am able to grab a single record and grouped records don't return the recursive stuff...which isn't needed until you're in a detail view.

Sweet feature.
#14

[eluser]WanWizard[/eluser]
Couldn't resist, and had a quick peek.

Unfortunately, it's a bit more complicated. When you don't pass any fields, to_json() defaults to the tables columns, which does not include the relations. So they will be skipped.

This also happened before the changes, so if you were getting related keys before, you must have passed a fieldlist?

Second issue is that even if you pass the fieldlist including the relations you're interested in, as soon as you recurse into the first relation, you no longer have a fieldlist that is usefull, so the buck will stop there.

Unless I use the $recurse switch to indicate all relations have to be included, which again might cause an awful lot of output on the wrong object.

I have to think this over... perhaps I have to use an array of relation names instead of a boolean...
#15

[eluser]WanWizard[/eluser]
New update is available on bitbucket.

I've opted to use the idea of an array of relations (using deep relationship notation), so you can selectively include them.

Code:
// load some related objects
$parent = new Parent();
$parent->get_by_id(1);
$parent->child->get();
$parent->child->grandchild->get();

// and dump them all
$json = $parent->all_to_json(null, false, array('child', 'child/grandchild'));




Theme © iAndrew 2016 - Forum software by © MyBB