Skip to content

Loading…

DDC-1884: leftJoin via composite key part not hydrated if joining table solely consists of identifiers #2548

Closed
doctrinebot opened this Issue · 5 comments

2 participants

@doctrinebot

Jira issue originally created by user scoolen:

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');
@doctrinebot

Comment created by @beberlei:

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](http://www.doctrine-project.org/jira/browse/DDC-1652) look like it could be related to your problem.

@doctrinebot

Comment created by scoolen:

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

@doctrinebot

Comment created by scoolen:

Added testcase on 2.1.x (not the right one unfortunately) branch: #395

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

@doctrinebot

Comment created by @beberlei:

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.

@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.3.5 milestone
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.