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

Closed
opened 2026-01-22 14:15:54 +01:00 by admin · 6 comments
Owner

Originally created by @doctrinebot on GitHub (Jul 27, 2013).

Originally assigned to: @Ocramius on GitHub.

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.

Originally created by @doctrinebot on GitHub (Jul 27, 2013). Originally assigned to: @Ocramius on GitHub. 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.
admin added the Improvement label 2026-01-22 14:15:54 +01:00
admin closed this issue 2026-01-22 14:15:54 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jul 27, 2013):

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 commented on GitHub (Jul 27, 2013): 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](http://www.doctrine-project.org/jira/browse/DDC-16)
Author
Owner

@doctrinebot commented on GitHub (Jul 27, 2013):

Comment created by @ocramius:

Related to DDC-16

@doctrinebot commented on GitHub (Jul 27, 2013): Comment created by @ocramius: Related to [DDC-16](http://www.doctrine-project.org/jira/browse/DDC-16)
Author
Owner

@doctrinebot commented on GitHub (Jul 27, 2013):

Issue was closed with resolution "Won't Fix"

@doctrinebot commented on GitHub (Jul 27, 2013): Issue was closed with resolution "Won't Fix"
Author
Owner

@doctrinebot commented on GitHub (Jul 27, 2013):

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 commented on GitHub (Jul 27, 2013): 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.
Author
Owner

@doctrinebot commented on GitHub (Jul 27, 2013):

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 commented on GitHub (Jul 27, 2013): 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.
Author
Owner

@doctrinebot commented on GitHub (Jul 27, 2013):

Comment created by nbottarini:

Thank you very much Marco!

@doctrinebot commented on GitHub (Jul 27, 2013): Comment created by nbottarini: Thank you very much Marco!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3227