Skip to content

Commit

Permalink
Merge pull request #710 from sandermarechal/extra-lazy-get-fix
Browse files Browse the repository at this point in the history
Fix extra lazy get
  • Loading branch information
beberlei committed Jun 30, 2013
2 parents 8e1111c + 06ed21e commit 69fe5c4
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 42 deletions.
1 change: 1 addition & 0 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ public function indexOf($element)
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'])
) {
Expand Down
18 changes: 0 additions & 18 deletions lib/Doctrine/ORM/Persisters/ManyToManyPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,6 @@
*/
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}
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Persisters/OneToManyPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function get(PersistentCollection $coll, $index)
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);
}

/**
Expand Down
24 changes: 1 addition & 23 deletions tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase
private $groupId;
private $articleId;

private $groupname;
private $topic;
private $phonenumber;

Expand Down Expand Up @@ -562,33 +561,13 @@ public function testGetIndexByOneToMany()
$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()
Expand Down Expand Up @@ -668,8 +647,7 @@ private function loadFixture()
$this->userId = $user1->getId();
$this->groupId = $group1->id;

$this->groupname = $group1->name;
$this->topic = $article1->topic;
$this->phonenumber = $phonenumber1->phonenumber;
}
}
}

0 comments on commit 69fe5c4

Please sign in to comment.