Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]davidMC1982[/eluser]
I think I've found a bug in the JSON extension. I believe the correct format for a JSON object is:

[{"key":"value", "key":"value"},{"key":"value", "key":"value"}]

whereas when using all_to_json() I get:

["{"key":"value", "key":"value"}","{"key":"value", "key":"value"}"]

Can anyone confirm this?

To get around it I'm using all_to_array() then the standard php json_encode().

[eluser]WanWizard[/eluser]
Which version of Datamapper are you using? Afaik this issue was fixed early January 2012...

You can get the latest version from bitbucket (https://bitbucket.org/wanwizard/datamapper/overview) to see if that fixes it.

[eluser]davidMC1982[/eluser]
Yes, that was it. My version was from commit a58c1b0c9927.

Thanks.

[eluser]darkylmnx[/eluser]
Hi, i was wondering if there was any kind of eager loading in DataMapper ? i didn't find anything about it and i tried "include_related" but without success.

Anyway ?

[eluser]davidMC1982[/eluser]
Check the config file:

auto_populate_has_many
auto_populate_has_one

This may be what you are looking for?

Otherwise, you could use something like $model->include_related($related_model_name, , , TRUE) which I believe would get the objects for $model and $related_model and allow you to access them like $model->related_model->field

[eluser]darkylmnx[/eluser]
Nope that didn't work,

Code:
$u = new User();
$u->include_related('playlists', '*', FALSE, TRUE)->get_where(array('id' => 1));
foreach($u->playlists as $p) var_dump($p->title);

[eluser]WanWizard[/eluser]
[quote author="darkylmnx" date="1360620735"]Hi, i was wondering if there was any kind of eager loading in DataMapper ? i didn't find anything about it and i tried "include_related" but without success.
[/quote]
If you mean by that populating related objects from a single query, then the answer is no.

Datamapper has been around from PHP4 days, times when instantiating objects was every expensive.

So it was decided to use include_related() and return the flattened result in a single object, instead of hydrating the result and creating all the related objects, which potentionally can make Datamapper very slow (depending on the number of joins and the number of records returned.

It's been on my todo list for quite some time, but as I haven't used CI in years, I don't really get around to new functionality anymore.

auto_populate does lazy loading, which is not the question.

Using TRUE as fourth parameter on include_related() should work, but not on all relation types. What was the result of your foreach?

[eluser]davidMC1982[/eluser]
[quote author="darkylmnx" date="1360622391"]Nope that didn't work,
[/quote]

You're trying to iterate over playlists but you have to iterate over your user objects. If you have multiple playlists per user, then you'll have duplicate user objects within your resultset.

Code:
$u = new User();
$u->include_related('playlists', '*', FALSE, TRUE)->get_where(array('id' => 1));
foreach($u as $user) var_dump($user->playlist->title);

Of course, I could be completely wrong. I haven't tried it.

Wanwizard, what is generally the best way to get a resultset that includes objects from a "has_many" related model, such that you can iterate over the relation on a per model basis. i.e. Like darkylmnx was trying above.

[eluser]darkylmnx[/eluser]
[quote author="WanWizard" date="1360623737"][quote author="darkylmnx" date="1360620735"]Hi, i was wondering if there was any kind of eager loading in DataMapper ? i didn't find anything about it and i tried "include_related" but without success.
[/quote]
If you mean by that populating related objects from a single query, then the answer is no.

Datamapper has been around from PHP4 days, times when instantiating objects was every expensive.

So it was decided to use include_related() and return the flattened result in a single object, instead of hydrating the result and creating all the related objects, which potentionally can make Datamapper very slow (depending on the number of joins and the number of records returned.

It's been on my todo list for quite some time, but as I haven't used CI in years, I don't really get around to new functionality anymore.

auto_populate does lazy loading, which is not the question.

Using TRUE as fourth parameter on include_related() should work, but not on all relation types. What was the result of your foreach?[/quote]

Not a good one lol,

i was expecting to have foreach User his playlists

it's a has_many relationship

User has_many Playlists
Playlists has_one User

The fact is that i want to get all the playlists of a specifique User but in one query

[eluser]darkylmnx[/eluser]
[quote author="davidMC1982" date="1360624775"][quote author="darkylmnx" date="1360622391"]Nope that didn't work,
[/quote]

You're trying to iterate over playlists but you have to iterate over your user objects. If you have multiple playlists per user, then you'll have duplicate user objects within your resultset.

Code:
$u = new User();
$u->include_related('playlists', '*', FALSE, TRUE)->get_where(array('id' => 1));
foreach($u as $user) var_dump($user->playlist->title);

Of course, I could be completely wrong. I haven't tried it.

Wanwizard, what is generally the best way to get a resultset that includes objects from a "has_many" related model, such that you can iterate over the relation on a per model basis. i.e. Like darkylmnx was trying above.[/quote]

in the shown example there's only one User (checkout the where clause)




Theme © iAndrew 2016 - Forum software by © MyBB