Skip to content

Commit

Permalink
Committing a single document keeps the change sets of the rest of the…
Browse files Browse the repository at this point in the history
… documents with NOTIFY change tracking
  • Loading branch information
rbrtbn committed Oct 29, 2015
1 parent cb01a38 commit 434045a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,32 @@ public function commit($document = null, array $options = array())
$this->documentUpserts =
$this->documentUpdates =
$this->documentDeletions =
$this->documentChangeSets =
$this->collectionUpdates =
$this->collectionDeletions =
$this->visitedCollections =
$this->scheduledForDirtyCheck =
$this->orphanRemovals =
$this->orphanRemovals =
$this->hasScheduledCollections = array();

// if not the whole unit of work is committed,
// then don't remove all the change sets
if (is_object($document)) {
$class = $this->dm->getClassMetadata(get_class($document));
$oid = spl_object_hash($document);

unset($this->documentChangeSets[$oid]);
unset($this->scheduledForDirtyCheck[$class->name][$oid]);
} elseif (is_array($document)) {
foreach ($document as $object) {
$class = $this->dm->getClassMetadata(get_class($object));
$oid = spl_object_hash($object);

unset($this->documentChangeSets[$oid]);
unset($this->scheduledForDirtyCheck[$class->name][$oid]);
}
} else {
$this->documentChangeSets = array();
$this->scheduledForDirtyCheck = array();
}
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,48 @@ public function testChangeTrackingNotify()
$this->assertTrue($updates[0] === $item);
}

public function testChangeTrackingNotifyWithMultipleDocuments()
{
// Preparations
$pb = $this->getMockPersistenceBuilder();

$class = $this->dm->getClassMetadata('Doctrine\ODM\MongoDB\Tests\NotifyChangedDocument');
$persister = $this->getMockDocumentPersister($pb, $class);
$this->uow->setDocumentPersister($class->name, $persister);

$entityA = new NotifyChangedDocument();
$entityA->setId(2);
$entityA->setData('A');
$this->uow->persist($entityA);

$entityB = new NotifyChangedDocument();
$entityB->setId(3);
$entityB->setData('B');
$this->uow->persist($entityB);

$this->uow->commit();

// Update both entities
$persister->reset();
$entityA->setData('A2');
$entityB->setData('B2');

// Committing $entityA should only update $entityA.
$this->uow->commit($entityA);
$updates = $persister->getUpdates();
$this->assertEquals(1, count($updates));
$this->assertEquals($updates[0], $entityA);
$this->assertTrue($this->uow->isScheduledForDirtyCheck($entityB));

// Committing the whole UnitOfWork should now also update $entityB.
$this->uow->commit();
$updates = $persister->getUpdates();
$this->assertEquals(2, count($persister->getUpdates()));
$this->assertEquals($updates[0], $entityA);
$this->assertEquals($updates[1], $entityB);
$this->assertFalse($this->uow->isScheduledForDirtyCheck($entityB));
}

public function testDoubleCommitWithChangeTrackingNotify()
{
$pb = $this->getMockPersistenceBuilder();
Expand Down

0 comments on commit 434045a

Please sign in to comment.