Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2131

Fetch join not working on class table inheritance

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 2.3
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      I have an entity Appointment, that is associated with application forms.
      I have an abstract class AbstractApplicationForm that has multiple (6) child classes.

      The mapping info for the abstract application form looks like this:

      AbstractApplicationForm.php
      /**
       * @ORM\Entity()
       * @ORM\Table("applicationform")
       * @ORM\InheritanceType("JOINED")
       * @ORM\DiscriminatorColumn(name="discr", type="string")
       * @ORM\DiscriminatorMap({
       *     "slc" = "SlcApplicationForm",
       *     "vsdb" = "VsdbApplicationForm",
       *     "vss" = "VssApplicationForm",
       *     "opzd" = "OpzdApplicationForm",
       *     "vzu" = "VzuApplicationForm",
       *     "opzdvzu" = "OpzdVzuApplicationForm"
       * })
       */
      abstract class AbstractApplicationForm
      ...
      

      I have an OneToMany connection from Appointment to AbstractApplicationForm.
      When i construct an DQL, to get an appointment with only specific application forms it gets wrong translated into SQL.

      For instance if the query builder looks like this:

      $qb->select('ap, af')
         ->from(Appointment::getClass(), 'ap')
         ->leftJoin('ap.applicationForms', 'af', 'WITH', 'af.id = 123456789')
         ->andWhere('ap.id = :appointment')
         ->setParameter('appointment', $appointment);
      

      So with this dql i should get an appointment with filtered application forms. In this case it whould return an appointment with only one application form (that with the id 123456789).
      But it returns all associated application form instead on only that with the id 123456789, because it ignores the with clause.

      Here is the SQL that gets generated from the DQL:

      SELECT ...
      FROM program_execution_activity_appointment p8_, program_execution_activity_appointment p0_
      LEFT JOIN applicationform a1_ ON p0_.id = a1_.appointment_id 
      LEFT JOIN applicationform_slc a2_ ON a1_.id = a2_.id 
      LEFT JOIN applicationform_vsdb a3_ ON a1_.id = a3_.id 
      LEFT JOIN applicationform_vss a4_ ON a1_.id = a4_.id 
      LEFT JOIN applicationform_opzd a5_ ON a1_.id = a5_.id 
      LEFT JOIN applicationform_vzu a6_ ON a1_.id = a6_.id 
      LEFT JOIN applicationform_opzdvzu a7_ ON a1_.id = a7_.id AND (a1_.id = 123456789) 
      WHERE p0_.id = 1
      

      The problem i see here is that the AND is added to the last join (applicationform_opzdvzu). But it should added to the first join (applicationform). Because the first join represents the parent entity (AbstractApplicationForm).
      There is also a problem in the FROM clause. The problem is that program_execution_activity_appointment p8_ is never used anywhere else, except in the from clause.

      The generated query should look like this:

      SELECT ...
      FROM program_execution_activity_appointment p0_
      LEFT JOIN applicationform a1_ ON p0_.id = a1_.appointment_id AND (a1_.id = 123456789) 
      LEFT JOIN applicationform_slc a2_ ON a1_.id = a2_.id
      LEFT JOIN applicationform_vsdb a3_ ON a1_.id = a3_.id
      LEFT JOIN applicationform_vss a4_ ON a1_.id = a4_.id
      LEFT JOIN applicationform_opzd a5_ ON a1_.id = a5_.id
      LEFT JOIN applicationform_vzu a6_ ON a1_.id = a6_.id
      LEFT JOIN applicationform_opzdvzu a7_ ON a1_.id = a7_.id
      WHERE p0_.id = 1
      

      This sql works as expected.

      I hope i was clear enough what the problem is.

        Issue Links

          Activity

            People

            • Assignee:
              Benjamin Eberlei
              Reporter:
              alsar
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: