Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1884

leftJoin via composite key part not hydrated if joining table solely consists of identifiers

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.2.0-RC1
    • Fix Version/s: 2.4, 2.3.5
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None
    • Environment:
      MAMP

      Description

      Suppose I have the following entities:

      /**
       * @ORM\Entity
       * @ORM\Table(name="driver")
       */
      class Driver
      {
          /**
           * @ORM\Id
           * @ORM\Column(type="integer")
           * @ORM\GeneratedValue(strategy="AUTO")
           */
          private $id;
          
          /**
           * @ORM\Column(type="string", length=255);
           */
          private $name;
          
          /**
           * @ORM\OneToMany(targetEntity="DriverRide", mappedBy="driver")
           */
          private $driverRides;
      }
      
      /**
       * @ORM\Entity
       * @ORM\Table(name="driver_ride")
       */
      class DriverRide
      {
          /**
           * @ORM\Id
           * @ORM\ManyToOne(targetEntity="Driver", inversedBy="driverRides")
           * @ORM\JoinColumn(name="driver_id", referencedColumnName="id")
           */
          private $driver;
          
          /**
           * @ORM\Id
           * @ORM\ManyToOne(targetEntity="Car", inversedBy="carRides")
           * @ORM\JoinColumn(name="car", referencedColumnName="brand")
           */
          private $car;
      }
      
      /**
       * @ORM\Entity
       * @ORM\Table(name="car")
       */
      class Car
      {
          /**
           * @ORM\Id
           * @ORM\Column(type="string", length=25)
           * @ORM\GeneratedValue(strategy="NONE")
           */
          private $brand;
          
          /**
           * @ORM\Column(type="string", length=255);
           */
          private $model;
          
          /**
           * @ORM\OneToMany(targetEntity="DriverRide", mappedBy="car")
           */
          private $carRides;
      }
      

      And want to query for Cars that a Driver drove in:

      $qb = $em->createQueryBuilder();
      
      $qb->select('d, dr, c')
         ->from('Driver', 'd')
         ->leftJoin('d.driverRides', 'dr')
         ->leftJoin('dr.car', 'c')
         ->where('d.id = ?1') /* some Driver id */
         ->getQuery()->getArrayResult();
      

      Expected results:
      I expect to get an array with an index 'driverRides' with an array of Cars (depending on the data of course).

      Actual result:
      Just an array with Driver data.

      When I started doing some testing I found out I get a different result when I add a third column to the DriverRide table that isn't part of the composite primary key.
      Now I did get a 'driverRides' array, but with just a single row and not three as I expected to get in my case.

      When I removed the composite key and used an auto-generated id-column, everything worked as expected.

      Some test data you might want to use:

      INSERT INTO `car` (`brand`, `model`) VALUES
      ('BMW', '7 Series'),
      ('Crysler', '300'),
      ('Mercedes', 'C-Class'),
      ('Volvo', 'XC90');
      
      INSERT INTO `driver` (`id`, `name`) VALUES
      (1, 'John Doe'),
      (2, 'Foo Bar');
      
      INSERT INTO `driver_ride` (`driver_id`, `car`) VALUES
      (1, 'Crysler'),
      (1, 'Mercedes'),
      (1, 'Volvo'),
      (2, 'BMW');
      

        Activity

        Hide
        Benjamin Eberlei added a comment -

        I upgraded the testcase to master locally, and it seems to fail on Array hydration only now, with a notice:

        Exception: [PHPUnit_Framework_Error] Argument 1 passed to Doctrine\ORM\Internal\Hydration\ArrayHydrator::updateResultPointer() must be of the type array, string given, called in /home/benny/code/php/workspace/doctrine2/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php on line 196 and defined
        

        I remember fixing something similar for ObjectHydration (which works for your testcases). Will investigate more when I have time.

        Show
        Benjamin Eberlei added a comment - I upgraded the testcase to master locally, and it seems to fail on Array hydration only now, with a notice: Exception: [PHPUnit_Framework_Error] Argument 1 passed to Doctrine\ORM\Internal\Hydration\ArrayHydrator::updateResultPointer() must be of the type array, string given, called in /home/benny/code/php/workspace/doctrine2/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php on line 196 and defined I remember fixing something similar for ObjectHydration (which works for your testcases). Will investigate more when I have time.
        Hide
        Sander Coolen added a comment - - edited

        Added testcase on 2.1.x (not the right one unfortunately) branch: https://github.com/doctrine/doctrine2/pull/395

        BTW I was adding said testcase on master and got an error similar to DDC-979

        Show
        Sander Coolen added a comment - - edited Added testcase on 2.1.x (not the right one unfortunately) branch: https://github.com/doctrine/doctrine2/pull/395 BTW I was adding said testcase on master and got an error similar to DDC-979
        Hide
        Sander Coolen added a comment -

        We're already using the 2.2.x-dev package. It does look similar to DDC-1652

        Show
        Sander Coolen added a comment - We're already using the 2.2.x-dev package. It does look similar to DDC-1652
        Hide
        Benjamin Eberlei added a comment -

        Can you update to at least 2.2.1 and try again, because this fix here http://www.doctrine-project.org/jira/browse/DDC-1652 look like it could be related to your problem.

        Show
        Benjamin Eberlei added a comment - Can you update to at least 2.2.1 and try again, because this fix here http://www.doctrine-project.org/jira/browse/DDC-1652 look like it could be related to your problem.

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Sander Coolen
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: