From 82f0c244e8b6bb6e6bba81a0cd0753ba566d09e3 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 19 Jun 2011 09:39:34 +0200 Subject: [PATCH] DDC-1189 - Bugfix with PersistentCollection#clear() in combination with lazy loading --- lib/Doctrine/ORM/PersistentCollection.php | 1 + .../ORM/Functional/BasicFunctionalTest.php | 30 ------------------- .../ManyToManyBasicAssociationTest.php | 20 +++++++++++++ 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index ce3c2e45d..82d616686 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -572,6 +572,7 @@ final class PersistentCollection implements Collection } } $this->coll->clear(); + $this->initialized = true; // direct call, {@link initialize()} is too expensive if ($this->association['isOwningSide']) { $this->changed(); $this->em->getUnitOfWork()->scheduleCollectionDeletion($this); diff --git a/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php b/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php index b6ca444ca..cb79795bc 100644 --- a/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php @@ -827,36 +827,6 @@ class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses where id=".$addressId."")); } - public function testClearingCollectionDoesNotInitialize() - { - $user = new CmsUser(); - $user->username = "beberlei"; - $user->name = "Benjamin E."; - $user->status = 'active'; - - $grp = new CmsGroup(); - $grp->setName("The Dudes"); - - $grp->addUser($user); - $user->addGroup($grp); - - $this->_em->persist($user); - $this->_em->persist($grp); - $this->_em->flush(); - $this->_em->clear(); - - $this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select count(*) from cms_users_groups")); - - $user2 = $this->_em->find(get_class($user), $user->id); - $this->assertFalse($user2->groups->isInitialized()); - $user2->groups->clear(); - $this->assertFalse($user2->groups->isInitialized()); - $this->_em->flush(); - $this->assertFalse($user2->groups->isInitialized()); - - $this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_users_groups")); - } - public function testGetPartialReferenceToUpdateObjectWithoutLoadingIt() { //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); diff --git a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php index c537b4c5f..5f97717d4 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php @@ -357,4 +357,24 @@ class ManyToManyBasicAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCa $this->_em->getUnitOfWork()->initializeObject($user->groups); $this->assertTrue($user->groups->isInitialized(), "Collection should be initialized after calling UnitOfWork::initializeObject()"); } + + /** + * @group DDC-1189 + * @group DDC-956 + */ + public function testClearBeforeLazyLoad() + { + $user = $this->addCmsUserGblancoWithGroups(4); + + $this->_em->clear(); + + $user = $this->_em->find(get_class($user), $user->id); + $user->groups->clear(); + $this->assertEquals(0, count($user->groups)); + + $this->_em->flush(); + + $user = $this->_em->find(get_class($user), $user->id); + $this->assertEquals(0, count($user->groups)); + } }