Browse files

Merge pull request #259 from danielholmes/m2m_extra_lazy_contains

Added fix for collection->contains with many-to-many extra lazy fetchMode
  • Loading branch information...
2 parents 2bb5115 + a12e5ac commit c1012f79706491e9dbf6c8741225b62833c6d2ce @guilhermeblanco guilhermeblanco committed Jan 16, 2012
View
6 lib/Doctrine/ORM/Persisters/ManyToManyPersister.php
@@ -254,7 +254,9 @@ public function contains(PersistentCollection $coll, $element)
$uow = $this->_em->getUnitOfWork();
// shortcut for new entities
- if ($uow->getEntityState($element, UnitOfWork::STATE_NEW) == UnitOfWork::STATE_NEW) {
+ $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
+ if ($entityState === UnitOfWork::STATE_NEW ||
+ ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element))) {
return false;
}
@@ -275,7 +277,7 @@ public function removeElement(PersistentCollection $coll, $element)
$uow = $this->_em->getUnitOfWork();
// shortcut for new entities
- if ($uow->getEntityState($element, UnitOfWork::STATE_NEW) == UnitOfWork::STATE_NEW) {
+ if ($uow->getEntityState($element, UnitOfWork::STATE_NEW) === UnitOfWork::STATE_NEW) {
return false;
}
View
55 tests/Doctrine/Tests/ORM/Functional/ManyToManyExtraLazyContainsTest.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Doctrine\Tests\ORM\Functional;
+
+use Doctrine\Common\Collections\ArrayCollection;
+require_once __DIR__ . '/../../TestInit.php';
+
+/**
+ *
+ */
+class ManyToManyExtraLazyContainsTest extends \Doctrine\Tests\OrmFunctionalTestCase
+{
+ public function setUp()
+ {
+ $this->useModelSet('company');
+ parent::setUp();
+ }
+
+ public function testManyToManyExtraLazyContainsAddedPendingInsertEntityIsTrue()
+ {
+ $contract = new \Doctrine\Tests\Models\Company\CompanyFlexContract();
+
+ $this->_em->persist($contract);
+ $this->_em->flush();
+
+ $this->_em->clear();
+ $contract = $this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexContract', $contract->getId());
+
+ $pendingInsertManager = new \Doctrine\Tests\Models\Company\CompanyManager();
+ $this->_em->persist($pendingInsertManager);
+ $contract->getManagers()->add($pendingInsertManager);
+
+ $result = $contract->getManagers()->contains($pendingInsertManager);
+
+ $this->assertTrue($result);
+ }
+
+ public function testManyToManyExtraLazyContainsNonAddedPendingInsertEntityIsFalse()
+ {
+ $contract = new \Doctrine\Tests\Models\Company\CompanyFlexContract();
+
+ $this->_em->persist($contract);
+ $this->_em->flush();
+
+ $this->_em->clear();
+ $contract = $this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexContract', $contract->getId());
+
+ $pendingInsertManager = new \Doctrine\Tests\Models\Company\CompanyManager();
+ $this->_em->persist($pendingInsertManager);
+
+ $result = $contract->getManagers()->contains($pendingInsertManager);
+
+ $this->assertFalse($result);
+ }
+}

0 comments on commit c1012f7

Please sign in to comment.