From c4b59b4eb046e4d472cacb87c5801ba661fc934a Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 24 Jan 2015 12:50:10 +0100 Subject: [PATCH] #1169 DDC-3343 - adapting patch to 2.4 branch --- .../ORM/Persisters/OneToManyPersister.php | 20 +++-- .../Functional/ExtraLazyCollectionTest.php | 84 ------------------- .../OneToManyExtraLazyTest.php | 82 ------------------ .../Doctrine/Tests/OrmFunctionalTestCase.php | 8 ++ 4 files changed, 23 insertions(+), 171 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php index 2915fd2ed..cf39436d4 100644 --- a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php @@ -19,6 +19,7 @@ namespace Doctrine\ORM\Persisters; +use Doctrine\Common\Proxy\Proxy; use Doctrine\ORM\PersistentCollection; use Doctrine\ORM\UnitOfWork; @@ -237,11 +238,20 @@ class OneToManyPersister extends AbstractCollectionPersister return false; } - $mapping = $coll->getMapping(); - $class = $this->em->getClassMetadata($mapping['targetEntity']); - $sql = 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform) - . ' WHERE ' . implode('= ? AND ', $class->getIdentifierColumnNames()) . ' = ?'; + $mapping = $coll->getMapping(); + $persister = $this->uow->getEntityPersister($mapping['targetEntity']); + $targetMetadata = $this->em->getClassMetadata($mapping['targetEntity']); - return (bool) $this->conn->executeUpdate($sql, $this->getDeleteRowSQLParameters($coll, $element)); + if ($element instanceof Proxy && ! $element->__isInitialized()) { + $element->__load(); + } + + // clearing owning side value + $targetMetadata->reflFields[$mapping['mappedBy']]->setValue($element, null); + + $this->uow->computeChangeSet($targetMetadata, $element); + $persister->update($element); + + return true; } } diff --git a/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php b/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php index 6005dda6c..0d2709e82 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php @@ -402,90 +402,6 @@ class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed."); } - /** - * @group DDC-2504 - */ - public function testRemovalOfManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt() - { - $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId); - $childClass = $this->_em->find(DDC2504ChildClass::CLASSNAME, $this->ddc2504ChildClassId); - - $queryCount = $this->getCurrentQueryCount(); - - $otherClass->childClasses->removeElement($childClass); - - $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.'); - - $this->assertEquals( - $queryCount + 1, - $this->getCurrentQueryCount(), - 'The owning side of the association is updated' - ); - - $this->assertFalse($otherClass->childClasses->contains($childClass)); - } - - /** - * @group DDC-2504 - */ - public function testRemovalOfNonManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt() - { - $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId); - $queryCount = $this->getCurrentQueryCount(); - - $otherClass->childClasses->removeElement(new DDC2504ChildClass()); - - $this->assertEquals( - $queryCount, - $this->getCurrentQueryCount(), - 'Removing an unmanaged entity should cause no query to be executed.' - ); - } - - /** - * @group DDC-2504 - */ - public function testRemovalOfNewElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt() - { - $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId); - $childClass = new DDC2504ChildClass(); - - $this->_em->persist($childClass); - - $queryCount = $this->getCurrentQueryCount(); - - $otherClass->childClasses->removeElement($childClass); - - $this->assertEquals( - $queryCount, - $this->getCurrentQueryCount(), - 'Removing a new entity should cause no query to be executed.' - ); - } - - /** - * @group DDC-2504 - */ - public function testRemovalOfNewManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt() - { - $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId); - $childClass = new DDC2504ChildClass(); - - $this->_em->persist($childClass); - $this->_em->flush(); - - $queryCount = $this->getCurrentQueryCount(); - - $otherClass->childClasses->removeElement($childClass); - - $this->assertEquals( - $queryCount, - $this->getCurrentQueryCount(), - 'No queries are executed, as the owning side of the association is not actually updated.' - ); - $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.'); - } - /** * */ diff --git a/tests/Doctrine/Tests/ORM/Functional/ValueConversionType/OneToManyExtraLazyTest.php b/tests/Doctrine/Tests/ORM/Functional/ValueConversionType/OneToManyExtraLazyTest.php index c323a44d8..a0d242fda 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ValueConversionType/OneToManyExtraLazyTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ValueConversionType/OneToManyExtraLazyTest.php @@ -4,7 +4,6 @@ namespace Doctrine\Tests\ORM\Functional\ValueConversionType; use Doctrine\Tests\Models\Tweet\Tweet; use Doctrine\Tests\Models\Tweet\User; -use Doctrine\Tests\Models\ValueConversionType as Entity; use Doctrine\Tests\OrmFunctionalTestCase; /** @@ -21,89 +20,8 @@ class OneToManyExtraLazyTest extends OrmFunctionalTestCase public function setUp() { $this->useModelSet('tweet'); - $this->useModelSet('vct_onetomany_extralazy'); parent::setUp(); - - $inversed = new Entity\InversedOneToManyExtraLazyEntity(); - $inversed->id1 = 'abc'; - - $owning1 = new Entity\OwningManyToOneExtraLazyEntity(); - $owning1->id2 = 'def'; - - $owning2 = new Entity\OwningManyToOneExtraLazyEntity(); - $owning2->id2 = 'ghi'; - - $owning3 = new Entity\OwningManyToOneExtraLazyEntity(); - $owning3->id2 = 'jkl'; - - $inversed->associatedEntities->add($owning1); - $owning1->associatedEntity = $inversed; - $inversed->associatedEntities->add($owning2); - $owning2->associatedEntity = $inversed; - $inversed->associatedEntities->add($owning3); - $owning3->associatedEntity = $inversed; - - $this->_em->persist($inversed); - $this->_em->persist($owning1); - $this->_em->persist($owning2); - $this->_em->persist($owning3); - - $this->_em->flush(); - $this->_em->clear(); - } - - public static function tearDownAfterClass() - { - $conn = static::$_sharedConn; - - $conn->executeUpdate('DROP TABLE vct_owning_manytoone_extralazy'); - $conn->executeUpdate('DROP TABLE vct_inversed_onetomany_extralazy'); - } - - public function testThatExtraLazyCollectionIsCounted() - { - $inversed = $this->_em->find( - 'Doctrine\Tests\Models\ValueConversionType\InversedOneToManyExtraLazyEntity', - 'abc' - ); - - $this->assertEquals(3, $inversed->associatedEntities->count()); - } - - public function testThatExtraLazyCollectionContainsAnEntity() - { - $inversed = $this->_em->find( - 'Doctrine\Tests\Models\ValueConversionType\InversedOneToManyExtraLazyEntity', - 'abc' - ); - - $owning = $this->_em->find( - 'Doctrine\Tests\Models\ValueConversionType\OwningManyToOneExtraLazyEntity', - 'def' - ); - - $this->assertTrue($inversed->associatedEntities->contains($owning)); - } - - public function testThatExtraLazyCollectionContainsAnIndexbyKey() - { - $inversed = $this->_em->find( - 'Doctrine\Tests\Models\ValueConversionType\InversedOneToManyExtraLazyEntity', - 'abc' - ); - - $this->assertTrue($inversed->associatedEntities->containsKey('def')); - } - - public function testThatASliceOfTheExtraLazyCollectionIsLoaded() - { - $inversed = $this->_em->find( - 'Doctrine\Tests\Models\ValueConversionType\InversedOneToManyExtraLazyEntity', - 'abc' - ); - - $this->assertCount(2, $inversed->associatedEntities->slice(0, 2)); } /** diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index d2a41cfb9..ff0f2cb53 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -139,6 +139,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\StockExchange\Stock', 'Doctrine\Tests\Models\StockExchange\Market', ), + 'tweet' => array( + 'Doctrine\Tests\Models\Tweet\Tweet', + 'Doctrine\Tests\Models\Tweet\User', + ), 'legacy' => array( 'Doctrine\Tests\Models\Legacy\LegacyUser', 'Doctrine\Tests\Models\Legacy\LegacyUserReference', @@ -269,6 +273,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase $conn->executeUpdate('DELETE FROM exchange_stocks'); $conn->executeUpdate('DELETE FROM exchange_markets'); } + if (isset($this->_usedModelSets['tweet'])) { + $conn->executeUpdate('DELETE FROM tweet_tweet'); + $conn->executeUpdate('DELETE FROM tweet_user'); + } if (isset($this->_usedModelSets['legacy'])) { $conn->executeUpdate('DELETE FROM legacy_users_cars'); $conn->executeUpdate('DELETE FROM legacy_users_reference');