From 5deebc8738fab0824955a7d59663b1e07e93b76b Mon Sep 17 00:00:00 2001
From: Daniel Holmes <daniel@creatio.com.au>
Date: Sun, 15 Jan 2012 23:25:57 +1100
Subject: [PATCH 1/2] Added fix for collection->contains when many-to-many
 extra lazy fetchMode

---
 .../ORM/Persisters/ManyToManyPersister.php    |  4 +-
 .../ManyToManyExtraLazyContainsTest.php       | 55 +++++++++++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 tests/Doctrine/Tests/ORM/Functional/ManyToManyExtraLazyContainsTest.php

diff --git a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php
index 33dab21d1..4286264f0 100644
--- a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php
+++ b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php
@@ -254,7 +254,9 @@ class ManyToManyPersister extends AbstractCollectionPersister
         $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;
         }
 
diff --git a/tests/Doctrine/Tests/ORM/Functional/ManyToManyExtraLazyContainsTest.php b/tests/Doctrine/Tests/ORM/Functional/ManyToManyExtraLazyContainsTest.php
new file mode 100644
index 000000000..bbb26fcea
--- /dev/null
+++ b/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);
+    }
+}
\ No newline at end of file

From a12e5ac8a719e332ca18e47c34a6832ac265b75b Mon Sep 17 00:00:00 2001
From: Daniel Holmes <daniel@creatio.com.au>
Date: Mon, 16 Jan 2012 08:12:11 +1100
Subject: [PATCH 2/2] Updated some comparisons to strict equality

---
 lib/Doctrine/ORM/Persisters/ManyToManyPersister.php | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php
index 4286264f0..54dc2141d 100644
--- a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php
+++ b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php
@@ -255,8 +255,8 @@ class ManyToManyPersister extends AbstractCollectionPersister
 
         // shortcut for new entities
         $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
-        if ($entityState == UnitOfWork::STATE_NEW || 
-            ($entityState == UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element))) {
+        if ($entityState === UnitOfWork::STATE_NEW || 
+            ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element))) {
             return false;
         }
 
@@ -277,7 +277,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
         $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;
         }