Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2574

Add posibility to fetch subclass associations in a polymorphic query

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: 2.3.4
    • Fix Version/s: None
    • Component/s: DQL, ORM
    • Security Level: All
    • Labels:

      Description

      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.

        Activity

        Hide
        Marco Pivetta added a comment -

        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

        Show
        Marco Pivetta added a comment - 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
        Hide
        Marco Pivetta added a comment -

        Related to DDC-16

        Show
        Marco Pivetta added a comment - Related to DDC-16
        Hide
        Nicolas Bottarini added a comment -

        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.

        Show
        Nicolas Bottarini added a comment - 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.
        Hide
        Marco Pivetta added a comment -

        Nicolas Bottarini 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.

        Show
        Marco Pivetta added a comment - Nicolas Bottarini 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.
        Hide
        Nicolas Bottarini added a comment -

        Thank you very much Marco!

        Show
        Nicolas Bottarini added a comment - Thank you very much Marco!

          People

          • Assignee:
            Marco Pivetta
            Reporter:
            Nicolas Bottarini
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: