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.
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
Related to DDC-16
Issue was closed with resolution "Won't Fix"
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.
[~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.
Thank you very much Marco!