From 54898eca606d257b51f94aafef849b37f003af85 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Thu, 17 Apr 2014 03:53:21 +0000 Subject: [PATCH] Added index by consideration when eagerly loading to-many associations. --- lib/Doctrine/ORM/Persisters/BasicEntityPersister.php | 6 +++++- .../Tests/ORM/Functional/Ticket/DDC440Test.php | 11 +++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 8604cc4fe..db1dfa482 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -1161,7 +1161,7 @@ class BasicEntityPersister implements EntityPersister $isAssocToOneInverseSide = $assoc['type'] & ClassMetadata::TO_ONE && ! $assoc['isOwningSide']; $isAssocFromOneEager = $assoc['type'] !== ClassMetadata::MANY_TO_MANY && $assoc['fetch'] === ClassMetadata::FETCH_EAGER; - + if ( ! ($isAssocFromOneEager || $isAssocToOneInverseSide)) { continue; } @@ -1192,6 +1192,10 @@ class BasicEntityPersister implements EntityPersister $association = $assoc; $joinCondition = array(); + if (isset($assoc['indexBy'])) { + $this->rsm->addIndexBy($assocAlias, $assoc['indexBy']); + } + if ( ! $assoc['isOwningSide']) { $eagerEntity = $this->em->getClassMetadata($assoc['targetEntity']); $association = $eagerEntity->getAssociationMapping($assoc['mappedBy']); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC440Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC440Test.php index d7ff7ad3b..f4f944ad5 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC440Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC440Test.php @@ -39,10 +39,12 @@ class DDC440Test extends \Doctrine\Tests\OrmFunctionalTestCase $client->setName('Client1'); $phone = new DDC440Phone; + $phone->setId(1); $phone->setNumber('418 111-1111'); $phone->setClient($client); $phone2 = new DDC440Phone; + $phone->setId(2); $phone2->setNumber('418 222-2222'); $phone2->setClient($client); @@ -56,8 +58,9 @@ class DDC440Test extends \Doctrine\Tests\OrmFunctionalTestCase $uw = $this->_em->getUnitOfWork(); $client = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC440Client', $id); $clientPhones = $client->getPhones(); - $p1 = $clientPhones[0]; - $p2 = $clientPhones[1]; + + $p1 = $clientPhones[1]; + $p2 = $clientPhones[2]; // Test the first phone. The assertion actually failed because original entity data is not set properly. // This was because it is also set as MainPhone and that one is created as a proxy, not the @@ -156,8 +159,8 @@ class DDC440Client */ protected $main_phone; /** - * @OneToMany(targetEntity="DDC440Phone", mappedBy="client", cascade={"persist", "remove"}, fetch="EAGER") - * @orderBy({"number"="ASC"}) + * @OneToMany(targetEntity="DDC440Phone", mappedBy="client", cascade={"persist", "remove"}, fetch="EAGER", indexBy="id") + * @OrderBy({"number"="ASC"}) */ protected $phones; /**