DDC-1681: loadRelated() - Method to efficiently load sets of related entities in "sub"-select strategies #2327

Open
doctrinebot opened this Issue Mar 5, 2012 · 5 comments

2 participants

@doctrinebot

Jira issue originally created by user @beberlei:

As per Request of Seldaek ;)

@doctrinebot

Comment created by seldaek:

Sample:

$result = $queryBuilder->select('a')->from('User', 'a')->getQuery()->getResult();
$result->loadRelated('roles'); // loads all a.roles

Would be the equivalent of:

$result = $queryBuilder->select('a, r')->from('User', 'a')->join('a.roles', 'r')->getQuery()->getResult();

Except that the above does one simple query without join, then one WHERE IN query with all ids from the collection.
The latter obviously does a join and retrieves everything in one - more complex - query.

Bonus points if you can loadRelated multiple relations at once.

@doctrinebot

Comment created by stof:

I see an issue here: if you do a WHERE IN with the multiple ids, how do you know which entity the role is related to ?

and btw, the interface you suggested above would require breaking the BC: $result is an array right now.

@doctrinebot

Comment created by seldaek:

The interface is just an example mimicking the way it worked in D1, take it with a grain of salt.

As for the implementation, if you assume the roles table has a userid and role column, then you can do WHERE userid IN (1, 2, 3) and you'll get back the user ids so you know where to attach them. It might still require some joining in some cases, but the point is to keep the joins out of the main query.

@doctrinebot

Comment created by @guilhermeblanco:

The one to be implemented would be:

$result = $queryBuilder->select('a')->from('User', 'a')->getQuery()->getResult();
$em->loadRelated($result, 'roles'); // loads all a.roles

The reason for that is not all the times you have a PersistentCollection. You may have an ArrayCollection too.
I just don't know yet how to handle array and ArrayCollection situations, since you may not know which class you're trying to fetch.
Maybe I can try to grab the first item of array and retrieve Association information from ClassMetadata retrieved via get_class on first item. That would solve the problem.

Any other ideas, feel free to give me.

@doctrinebot

Comment created by sirian:

Any news? it's really cool to use "WHERE primary_key IN " instead of joins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment