Skip to content

Commit

Permalink
Fix issue when using notify tracking policy with multiple flush on en…
Browse files Browse the repository at this point in the history
…tity
  • Loading branch information
xhuberty committed Jan 5, 2016
1 parent 3ca6828 commit f09689c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
40 changes: 36 additions & 4 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function commit($entity = null)
}

// Compute changes done since last commit.
if ($entity === null) {
if (null === $entity) {
$this->computeChangeSets();
} elseif (is_object($entity)) {
$this->computeSingleEntityChangeSet($entity);
Expand Down Expand Up @@ -418,17 +418,40 @@ public function commit($entity = null)

$this->dispatchPostFlushEvent();

$this->postCommitClear($entity);
}

/**
* @param null|object|array $entity
*/
private function postCommitClear($entity = null)
{

// Clear up
$this->entityInsertions =
$this->entityUpdates =
$this->entityDeletions =
$this->extraUpdates =
$this->entityChangeSets =
$this->collectionUpdates =
$this->collectionDeletions =
$this->visitedCollections =
$this->scheduledForSynchronization =
$this->orphanRemovals = array();

if (null === $entity) {
$this->entityChangeSets = $this->scheduledForSynchronization = array();
return;
}

if (is_object($entity)) {
$entity = [$entity];
}

foreach ($entity as $object) {
$oid = spl_object_hash($object);
$class = $this->em->getClassMetadata(get_class($object));
$this->clearEntityChangeSet($oid);
$this->clearScheduledForSynchronization($class, $oid);
}
}

/**
Expand Down Expand Up @@ -3111,7 +3134,16 @@ public function registerManaged($entity, array $id, array $data)
*/
public function clearEntityChangeSet($oid)
{
$this->entityChangeSets[$oid] = array();
unset($this->entityChangeSets[$oid]);
}

/**
* @param $class
* @param string $oid
*/
public function clearScheduledForSynchronization($class, $oid)
{
unset($this->scheduledForSynchronization[$class->rootEntityName][$oid]);
}

/* PropertyChangedListener implementation */
Expand Down
24 changes: 23 additions & 1 deletion tests/Doctrine/Tests/ORM/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,29 @@ public function testChangeTrackingNotify()
$this->_unitOfWork->persist($entity);

$this->_unitOfWork->commit();
$this->assertEquals(1, count($persister->getInserts()));
$this->assertCount(1, $persister->getInserts());

// Create and Set first entity
$entity1 = new NotifyChangedEntity;
$entity1->setData('thedata');
$this->_unitOfWork->persist($entity1);

// Create and Set second entity
$entity2 = new NotifyChangedEntity;
$entity2->setData('thedata');
$this->_unitOfWork->persist($entity2);

$this->_unitOfWork->commit($entity1);
$this->assertCount(1, $this->_unitOfWork->getEntityChangeSet($entity2));

// Create and Set third entity
$entity3 = new NotifyChangedEntity;
$entity3->setData('thedata');
$this->_unitOfWork->persist($entity3);

$this->_unitOfWork->commit([$entity1,$entity2]);
$this->assertCount(1, $this->_unitOfWork->getEntityChangeSet($entity3));

$persister->reset();

$this->assertTrue($this->_unitOfWork->isInIdentityMap($entity));
Expand Down

0 comments on commit f09689c

Please sign in to comment.