CodeIgniter Forums
DataMapper ORM v1.8.2 - 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 ORM v1.8.2 (/showthread.php?tid=47243)



DataMapper ORM v1.8.2 - El Forum - 02-12-2013

[eluser]davidMC1982[/eluser]
Yes, but include_related() creates a full join on the tables so you'll have duplicate user entries per playlist.

To do what you want you could try any of the following:

Code:
//This is two queries
$user->where('id', 1)->get()->playlists->get();

//One query, but you won't have the user object
$playlists->where('user_id', 1)->get();

//Or
$playlists->where_related('user', 'id', 1)->get();

//Or
$playlists->where_related_user('id', 1)->get();


//This might give you the user object also. I haven't checked
$user = new User();
$playlists = new Playlist();
$playlists->get_by_related($user, 'id', 1);

//Or if you already have a $user object with an id
$playlists->get_by_related($user);



DataMapper ORM v1.8.2 - El Forum - 02-12-2013

[eluser]darkylmnx[/eluser]
[quote author="davidMC1982" date="1360659658"]Yes, but include_related() creates a full join on the tables so you'll have duplicate user entries per playlist.

To do what you want you could try any of the following:

Code:
//This is two queries
$user->where('id', 1)->get()->playlists->get();

//One query, but you won't have the user object
$playlists->where('user_id', 1)->get();

//Or
$playlists->where_related('user', 'id', 1)->get();

//Or
$playlists->where_related_user('id', 1)->get();


//This might give you the user object also. I haven't checked
$user = new User();
$playlists = new Playlist();
$playlists->get_by_related($user, 'id', 1);

//Or if you already have a $user object with an id
$playlists->get_by_related($user);
[/quote]

i will try but i don't think that will work (in the query yes) i will not be able to access the data, i tried and it seems it doesn't work


DataMapper ORM v1.8.2 - El Forum - 02-12-2013

[eluser]WanWizard[/eluser]
get_by_related should work, as it queries the FK.

The above will generate

Code:
SELECT * FROM playlist WHERE user_id = 1;



DataMapper ORM v1.8.2 - El Forum - 02-12-2013

[eluser]darkylmnx[/eluser]
[quote author="WanWizard" date="1360674590"]get_by_related should work, as it queries the FK.

The above will generate

Code:
SELECT * FROM playlist WHERE user_id = 1;
[/quote]

yes it will work but it will not eager load the relations it will load the playlist only not with the user with it

Ex :

We have 10 Users
each User has 5 Playlists

with Datamaper it will do a query with n+1 at each loop

i will 1 query for all users + 10 queries to have each of their playlists

Code:
$u = new User();
$u = $u->get();

foreach($u as $one) {
   $one->playlists->get()

  // or with auto populate

  $one->playlists
}

but the results will be

Code:
SELECT * FROM users

+

SELECT * FROM playlists WHERE user_id = 1
SELECT * FROM playlists WHERE user_id = 2
...
...
SELECT * FROM playlists WHERE user_id = 10

while with eager loading i expect something like

Code:
SELECT * FROM users
SELECT * FROM playlists WHERE user_id IN (1,2,3,4,5,6,7,8,9,10)

OR

SELECT [fields prefixed by tables with alias] FROM users LEFT JOIN playlists ON users.id = playlists.user_id

and then the parse/seperation would be done in PHP by the orm

checkout :

activerecord eager loading

OR

FuelPHP's built in ORM

with include_related it does the join but that data can't be accessed... it gives empty array() while the when i take the query generated and put it in PhpMyAdmin and execute it, it shows the expected results


DataMapper ORM v1.8.2 - El Forum - 02-13-2013

[eluser]WanWizard[/eluser]
<rant>
I hate this forum. this is the third time I type this message, I hope it will not be rejected now...
</rant>

I know what eager loading is, and how FuelPHP's ORM does it. It's my ORM. Smile

Looking back at your example, I think the issue is that you used FALSE as third parameter on the include_related().

This MUST be TRUE, as it will prefix the columns in the join by the relationship name. Which is required, both to prevent column name collisions (to make sure all columns are present in the result), and to allow Datamapper to figure out which column in the result belongs to which object. In a complex include_related(), you could have 6 columns in the result called 'id'...


DataMapper ORM v1.8.2 - El Forum - 02-13-2013

[eluser]Maglok[/eluser]
Quick one. Everytime I try to save a advanced relationship many to one it won't save the new object. It does this if I make a barebone test and I can reproduce it. Though the depth of the datamapper->save() function is going a bit deep for me.

Example:

Code:
$organisation = new Organisation();
  $organisation->get_by_id(2);
  
  $address = new Address();
  $address->type = 'post';
  $address->address = 'Testlaan test';
  
  //$address->save();
  
  $organisation->save_addresses($address);

It does work if I do a save() on the new Address first, but the docs tell me I should be able to do it with one save_addresses() call. Oddly enough many to many does not seem to have this.

EDIT: I also have no validations going and I tried skipping the validation specifically for this purpose.


DataMapper ORM v1.8.2 - El Forum - 02-13-2013

[eluser]darkylmnx[/eluser]
[quote author="WanWizard" date="1360745484"]<rant>
I hate this forum. this is the third time I type this message, I hope it will not be rejected now...
</rant>

I know what eager loading is, and how FuelPHP's ORM does it. It's my ORM. Smile

Looking back at your example, I think the issue is that you used FALSE as third parameter on the include_related().

This MUST be TRUE, as it will prefix the columns in the join by the relationship name. Which is required, both to prevent column name collisions (to make sure all columns are present in the result), and to allow Datamapper to figure out which column in the result belongs to which object. In a complex include_related(), you could have 6 columns in the result called 'id'...[/quote]

Lol, i'm sorry if i'm just making you repeat yourself Wink

As to say, i'v already used the used TRUR as third parameter but that only works for one - to - one

cf: http://datamapper.wanwizard.eu/pages/getadvanced.html#include_related

Code:
// Create User
$u = new User();

// add the group id and name to all users returned
$u->include_related('group', array('id', 'name'))->get();

foreach($u as $user) {
    echo("{$user->group_name} ({$user->group_id})\n");
}

in the present case it wount work if a user has multiple group name, how would he pass through them ? as here it takes the first one. (i tried a foreach on the group object but it prints out nothing (group here == to playlists in my one script))

Regards, sorry for taking your time so much Smile
(and sorry for my english, i'm french)


DataMapper ORM v1.8.2 - El Forum - 02-13-2013

[eluser]coldscooter[/eluser]
I want to be able to have multiple classes named the same name within my models folder.

Datamapper allows for the following syntax:

$test = new Test();

How can i limit where the code will look for this class, so that i can have other subfolders within my models folder containing a class of the same name?


DataMapper ORM v1.8.2 - El Forum - 02-13-2013

[eluser]darkylmnx[/eluser]
[quote author="coldscooter" date="1360786009"]I want to be able to have multiple classes named the same name within my models folder.

Datamapper allows for the following syntax:

$test = new Test();

How can i limit where the code will look for this class, so that i can have other subfolders within my models folder containing a class of the same name?[/quote]

i'm not sure i understood your question but i guess you need name spacing to do that (as from PHP 5.3)

EX :
Code:
$test = new Test();
$test1 = new Subfolder1\Test();
$test2 = new Subfolder2\Test();



DataMapper ORM v1.8.2 - El Forum - 02-13-2013

[eluser]WanWizard[/eluser]
[quote author="Maglok" date="1360749379"]Everytime I try to save a advanced relationship many to one it won't save the new object.[/quote]

I've seen similar reports about this in the past, but i haven't been able to find it.

Perhaps you can have a look at what goes wrong in your particular case. Related has_one's are processed in _save_relation(), in the "else if" that starts at L#5146...