From 5635fa60a4bea0580b969a5cd9235e2ae6e18a78 Mon Sep 17 00:00:00 2001 From: Sander Marechal Date: Thu, 27 Jun 2013 14:17:41 +0200 Subject: [PATCH 1/2] Check owning entitiy on extra lazy get with OneToMany relation --- lib/Doctrine/ORM/Persisters/OneToManyPersister.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php index 120ad548d..2915fd2ed 100644 --- a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php @@ -47,7 +47,7 @@ class OneToManyPersister extends AbstractCollectionPersister throw new \BadMethodCallException("Selecting a collection by index is only supported on indexed collections."); } - return $persister->load(array($mapping['indexBy'] => $index), null, null, array(), 0, 1); + return $persister->load(array($mapping['mappedBy'] => $coll->getOwner(), $mapping['indexBy'] => $index), null, null, array(), 0, 1); } /** From 06ed21e88303b41381d7b7488033bcef983277dd Mon Sep 17 00:00:00 2001 From: Sander Marechal Date: Thu, 27 Jun 2013 14:19:39 +0200 Subject: [PATCH 2/2] Remove extra-lazy-get for ManyToMany relation --- lib/Doctrine/ORM/PersistentCollection.php | 1 + .../ORM/Persisters/ManyToManyPersister.php | 18 -------------- .../Functional/ExtraLazyCollectionTest.php | 24 +------------------ 3 files changed, 2 insertions(+), 41 deletions(-) diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index a50cd8343..947774542 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -518,6 +518,7 @@ final class PersistentCollection implements Collection, Selectable public function get($key) { if ( ! $this->initialized + && $this->association['type'] === Mapping\ClassMetadataInfo::ONE_TO_MANY && $this->association['fetch'] === Mapping\ClassMetadataInfo::FETCH_EXTRA_LAZY && isset($this->association['indexBy']) ) { diff --git a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php index df46aa30a..d9f7e30ca 100644 --- a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php @@ -33,24 +33,6 @@ use Doctrine\ORM\UnitOfWork; */ class ManyToManyPersister extends AbstractCollectionPersister { - /** - * {@inheritdoc} - * - * @override - */ - public function get(PersistentCollection $coll, $index) - { - $mapping = $coll->getMapping(); - $uow = $this->em->getUnitOfWork(); - $persister = $uow->getEntityPersister($mapping['targetEntity']); - - if (!isset($mapping['indexBy'])) { - throw new \BadMethodCallException("Selecting a collection by index is only supported on indexed collections."); - } - - return $persister->load(array($mapping['indexBy'] => $index), null, null, array(), 0, 1); - } - /** * {@inheritdoc} * diff --git a/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php b/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php index 5d8fa0f2a..eb4dbe4d8 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php @@ -17,7 +17,6 @@ class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase private $groupId; private $articleId; - private $groupname; private $topic; private $phonenumber; @@ -562,33 +561,13 @@ class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertSame($article, $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $this->articleId)); } - /** - * @group DDC-1398 - */ - public function testGetIndexByManyToMany() - { - $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId); - /* @var $user CmsUser */ - - $queryCount = $this->getCurrentQueryCount(); - - $group = $user->groups->get($this->groupname); - - $this->assertFalse($user->groups->isInitialized()); - $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount()); - $this->assertSame($group, $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId)); - } - /** * @group DDC-1398 */ public function testGetNonExistentIndexBy() { $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId); - /* @var $user CmsUser */ - $this->assertNull($user->articles->get(-1)); - $this->assertNull($user->groups->get(-1)); } private function loadFixture() @@ -668,8 +647,7 @@ class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->userId = $user1->getId(); $this->groupId = $group1->id; - $this->groupname = $group1->name; $this->topic = $article1->topic; $this->phonenumber = $phonenumber1->phonenumber; } -} \ No newline at end of file +}