Skip to content

Commit

Permalink
Merge pull request #1339 from malarzm/changeset-coll
Browse files Browse the repository at this point in the history
Include Embed/RefMany in changeset
  • Loading branch information
malarzm committed Jan 16, 2016
2 parents 9b6aac9 + 81f068d commit 3e39020
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
11 changes: 8 additions & 3 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,13 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $r

// skip if value has not changed
if ($orgValue === $actualValue) {
// but consider dirty GridFSFile instances as changed
if ( ! (isset($class->fieldMappings[$propName]['file']) && $actualValue->isDirty())) {
if ($actualValue instanceof PersistentCollection) {
if (! $actualValue->isDirty() && ! $this->isCollectionScheduledForDeletion($actualValue)) {
// consider dirty collections as changed as well
continue;
}
} elseif ( ! (isset($class->fieldMappings[$propName]['file']) && $actualValue->isDirty())) {
// but consider dirty GridFSFile instances as changed
continue;
}
}
Expand Down Expand Up @@ -778,7 +783,7 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $r
if ($actualValue && $actualValue->isDirty() && CollectionHelper::usesSet($class->fieldMappings[$propName]['strategy'])) {
continue;
}
if ($orgValue instanceof PersistentCollection) {
if ($orgValue !== $actualValue && $orgValue instanceof PersistentCollection) {
$this->scheduleCollectionDeletion($orgValue);
}
continue;
Expand Down
78 changes: 72 additions & 6 deletions tests/Doctrine/ODM/MongoDB/Tests/Events/PreUpdateEventArgsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
use Doctrine\ODM\MongoDB\Event\PreUpdateEventArgs;
use Doctrine\ODM\MongoDB\Events;
use Documents\Article;
use Documents\Book;
use Documents\Chapter;
use Documents\Page;

class PreUpdateEventArgsTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
public function setUp()
{
parent::setUp();
$this->dm->getEventManager()->addEventListener(Events::preUpdate, $this);
}

public function testChangeSetIsUpdated()
{
$this->dm->getEventManager()->addEventListener(Events::preUpdate, new ChangeSetIsUpdatedListener());

$a = new Article();
$a->setTitle('Title');
$this->dm->persist($a);
Expand All @@ -27,8 +26,75 @@ public function testChangeSetIsUpdated()
$this->assertEquals('Changed', $a->getBody());
}

public function testCollectionsAreInChangeSet()
{
$listener = new CollectionsAreInChangeSetListener($this);
$this->dm->getEventManager()->addEventListener(Events::preUpdate, $listener);

$book = new Book();
$book->chapters->add($chapter = new Chapter('A'));
$chapter->pages->add(new Page(1));
$chapter->pages->add(new Page(2));

$this->dm->persist($book);
$this->dm->flush();

$book->chapters->add($chapter2 = new Chapter('B'));
$chapter->pages->add(new Page(3));
$this->dm->flush();

$listener->checkOnly([ Chapter::class ]);
unset($chapter->pages[0]);
$this->dm->flush();

$listener->checkOnly([ Book::class ]);

$book->chapters->removeElement($chapter2);
$this->dm->flush();

$book->chapters->clear();
$this->dm->flush();
}
}

class ChangeSetIsUpdatedListener
{
public function preUpdate(PreUpdateEventArgs $e)
{
$e->setNewValue('body', 'Changed');
}
}

class CollectionsAreInChangeSetListener
{
private $allowed;

private $phpunit;

public function __construct(PreUpdateEventArgsTest $phpunit)
{
$this->allowed = [ Book::class, Chapter::class ];
$this->phpunit = $phpunit;
}

public function checkOnly(array $allowed)
{
$this->allowed = $allowed;
}

public function preUpdate(PreUpdateEventArgs $e)
{
switch (get_class($e->getDocument())) {
case Book::class:
if (in_array(Book::class, $this->allowed)) {
$this->phpunit->assertTrue($e->hasChangedField('chapters'));
}
break;
case Chapter::class:
if (in_array(Chapter::class, $this->allowed)) {
$this->phpunit->assertTrue($e->hasChangedField('pages'));
}
break;
}
}
}

0 comments on commit 3e39020

Please sign in to comment.