Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DDC-1496 - Fix bug with OneToMany collections having orphanRemoval=tr…

…ue and Collection#clear() being called.
  • Loading branch information...
commit ceadc95439bd84c4d0bd955b8f2959ab4eb08207 1 parent 61e371c
@beberlei beberlei authored
View
3  lib/Doctrine/ORM/PersistentCollection.php
@@ -567,6 +567,9 @@ public function clear()
return;
}
if ($this->association['type'] == ClassMetadata::ONE_TO_MANY && $this->association['orphanRemoval']) {
+ // we need to initialize here, as orphan removal acts like implicit cascadeRemove,
+ // hence for event listeners we need the objects in memory.
+ $this->initialize();
foreach ($this->coll as $element) {
$this->em->getUnitOfWork()->scheduleOrphanRemoval($element);
}
View
53 tests/Doctrine/Tests/ORM/Functional/OneToManyOrphanRemovalTest.php
@@ -13,46 +13,63 @@
*/
class OneToManyOrphanRemovalTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
+ protected $userId;
+
protected function setUp()
{
$this->useModelSet('cms');
-
+
parent::setUp();
- }
-
- public function testOrphanRemoval()
- {
+
$user = new CmsUser;
$user->status = 'dev';
$user->username = 'romanb';
$user->name = 'Roman B.';
-
+
$phone = new CmsPhonenumber;
$phone->phonenumber = '123456';
-
+
$user->addPhonenumber($phone);
-
+
$this->_em->persist($user);
$this->_em->flush();
-
- $userId = $user->getId();
-
+
+ $this->userId = $user->getId();
$this->_em->clear();
-
- $userProxy = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $userId);
-
+ }
+
+ public function testOrphanRemoval()
+ {
+ $userProxy = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
+
$this->_em->remove($userProxy);
$this->_em->flush();
$this->_em->clear();
-
+
$query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u');
$result = $query->getResult();
-
+
$this->assertEquals(0, count($result), 'CmsUser should be removed by EntityManager');
-
+
+ $query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p');
+ $result = $query->getResult();
+
+ $this->assertEquals(0, count($result), 'CmsPhonenumber should be removed by orphanRemoval');
+ }
+
+ /**
+ * @group DDC-1496
+ */
+ public function testOrphanRemovalUnitializedCollection()
+ {
+ $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
+
+ $user->phonenumbers->clear();
+ $this->_em->flush();
+
$query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p');
$result = $query->getResult();
-
+
$this->assertEquals(0, count($result), 'CmsPhonenumber should be removed by orphanRemoval');
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.