DDC-2574: Add posibility to fetch subclass associations in a polymorphic query #3302

Closed
doctrinebot opened this Issue Jul 27, 2013 · 6 comments

2 participants

@doctrinebot

Jira issue originally created by user nbottarini:

Suppose you have a parent class A and a subclass B that has an association with class C.
If you want to make a polymorphic query on all instances of class A the relation on class C in the subclass B always returns a proxy.
There's not posibility to use a left join with class C to fetch that association. This causes a N+1 SELECT performance problem.

@doctrinebot

Comment created by @ocramius:

This is a known/wanted limitation. Fetch-joining on associations of subclasses is not supported anyway, since from a DQL perspective, these associations don't exist at all. It's a very old won't fix: see DDC-16

@doctrinebot

Comment created by @ocramius:

Related to DDC-16

@doctrinebot

Issue was closed with resolution "Won't Fix"

@doctrinebot

Comment created by nbottarini:

Hi Marco, thanks for the fast response!
Suppose you have a base Notification class and then a subclass for each type of concrete notification (UserFollowNotification, ProductAddedNotification, etc etc). You can't show the user's notifications with each notification detail without incurring in a n+1 performance problem. How do you deal with this kind of situations in doctrine?. In Nhibernate, for example, you can choose that an association must always be fetched at mapping level or at query level (with DataLoading options).
How do you handle this situations in doctrine?

Thank you very much.

@doctrinebot

Comment created by @ocramius:

[~nbottarini] assuming that you want to work only on a particular subclass, you may want to refresh a set of objects by building a specific DQL query.

First, retrieve all your records that contain the said collection. Then create following query:

SELECT f, b FROM Foo f LEFT JOIN f.bar b WHERE f.id IN (:foos)

You can then run this query against the objects you fetched:

$refreshedFoos = $query->setParameter('foos', $fetchedObjects)->setHint(\Doctrine\ORM\Query::HINT_REFRESH, true)->getResult();

This should reduce the overhead greatly.

@doctrinebot

Comment created by nbottarini:

Thank you very much Marco!

@Ocramius Ocramius was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment