Skip to content

Commit

Permalink
Merge pull request #7861 from ferrastas/bug_removing_collection
Browse files Browse the repository at this point in the history
Delete statements will not be created using `clear`
  • Loading branch information
lcobucci committed Nov 15, 2019
2 parents c83094b + 7d77984 commit 6a827d5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
4 changes: 1 addition & 3 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,7 @@ public function clear()
if ($this->association['isOwningSide'] && $this->owner) {
$this->changed();

if (! $this->em->getClassMetadata(get_class($this->owner))->isChangeTrackingDeferredExplicit()) {
$uow->scheduleCollectionDeletion($this);
}
$uow->scheduleCollectionDeletion($this);

$this->takeSnapshot();
}
Expand Down
14 changes: 13 additions & 1 deletion lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use InvalidArgumentException;
use Throwable;
use UnexpectedValueException;
use function get_class;

/**
* The UnitOfWork is responsible for tracking changes to objects during an
Expand Down Expand Up @@ -380,7 +381,18 @@ public function commit($entity = null)
try {
// Collection deletions (deletions of complete collections)
foreach ($this->collectionDeletions as $collectionToDelete) {
$this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete);
if (! $collectionToDelete instanceof PersistentCollection) {
$this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete);

continue;
}

// Deferred explicit tracked collections can be removed only when owning relation was persisted
$owner = $collectionToDelete->getOwner();

if ($this->em->getClassMetadata(get_class($owner))->isChangeTrackingDeferredImplicit() || $this->isScheduledForDirtyCheck($owner)) {
$this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete);
}
}

if ($this->entityInsertions) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7761Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,23 @@ public function testCollectionClearDoesNotClearIfNotPersisted() : void

$entity = $this->_em->find(GH7761Entity::class, 1);
self::assertCount(1, $entity->children);
}

/**
* @group GH-7862
*/
public function testCollectionClearDoesClearIfPersisted() : void
{
/** @var GH7761Entity $entity */
$entity = $this->_em->find(GH7761Entity::class, 1);
$entity->children->clear();
$this->_em->persist($entity);
$this->_em->flush();

$this->_em->clear();

$entity = $this->_em->find(GH7761Entity::class, 1);
self::assertCount(0, $entity->children);
}
}

Expand Down
12 changes: 0 additions & 12 deletions tests/Doctrine/Tests/ORM/PersistentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,4 @@ public function testModifyUOWForDeferredImplicitOwnerOnClear() : void

$this->collection->clear();
}

public function testDoNotModifyUOWForDeferredExplicitOwnerOnClear() : void
{
$unitOfWork = $this->createMock(UnitOfWork::class);
$unitOfWork->expects(self::never())->method('scheduleCollectionDeletion');
$this->_emMock->setUnitOfWork($unitOfWork);

$classMetaData = $this->_emMock->getClassMetadata(ECommerceCart::class);
$classMetaData->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_DEFERRED_EXPLICIT);

$this->collection->clear();
}
}

0 comments on commit 6a827d5

Please sign in to comment.