Skip to content

Commit 1d44ecc

Browse files
authored
Merge pull request #1437 from malarzm/changeset-fix
Fix changeset missing old value for exchanged associations
2 parents e37325a + 9bffdba commit 1d44ecc

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

lib/Doctrine/ODM/MongoDB/UnitOfWork.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,11 @@ function ($assoc) { return empty($assoc['notSaved']); }
840840
$oid2 = spl_object_hash($obj);
841841

842842
if (isset($this->documentChangeSets[$oid2])) {
843-
$this->documentChangeSets[$oid][$mapping['fieldName']] = array($value, $value);
843+
if (empty($this->documentChangeSets[$oid][$mapping['fieldName']])) {
844+
// instance of $value is the same as it was previously otherwise there would be
845+
// change set already in place
846+
$this->documentChangeSets[$oid][$mapping['fieldName']] = array($value, $value);
847+
}
844848

845849
if ( ! $isNewDocument) {
846850
$this->scheduleForUpdate($document);

tests/Doctrine/ODM/MongoDB/Tests/Functional/CollectionsTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Doctrine\ODM\MongoDB\Tests\Functional;
44

5+
use Doctrine\Common\Collections\ArrayCollection;
56
use Documents\Bars\Bar;
67
use Documents\Bars\Location;
78
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
@@ -65,6 +66,14 @@ public function testCollections()
6566
$bar = $this->dm->find('Documents\Bars\Bar', $bar->getId());
6667
$locations = $bar->getLocations();
6768
$this->assertEquals(0, count($locations));
69+
$this->dm->flush();
70+
71+
$bar->setLocations(new ArrayCollection([ new Location('Cracow') ]));
72+
$this->uow->computeChangeSets();
73+
$changeSet = $this->uow->getDocumentChangeSet($bar);
74+
$this->assertNotEmpty($changeSet['locations']);
75+
$this->assertSame($locations, $changeSet['locations'][0]);
76+
$this->assertSame($bar->getLocations(), $changeSet['locations'][1]);
6877
}
6978

7079
public function testCreateCollections()

tests/Doctrine/ODM/MongoDB/Tests/Functional/EmbeddedTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ public function testOneEmbedded()
108108
->getSingleResult();
109109
$this->assertNotNull($user);
110110
$this->assertEquals($addressClone, $user->getAddress());
111+
112+
$oldAddress = $user->getAddress();
113+
$address = new Address();
114+
$address->setAddress('Someplace else');
115+
$user->setAddress($address);
116+
$this->uow->computeChangeSets();
117+
$changeSet = $this->uow->getDocumentChangeSet($user);
118+
$this->assertNotEmpty($changeSet['address']);
119+
$this->assertSame($oldAddress, $changeSet['address'][0]);
120+
$this->assertSame($user->getAddress(), $changeSet['address'][1]);
111121
}
112122

113123
public function testRemoveOneEmbedded()

tests/Documents/Bars/Bar.php

+5
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ public function getLocations()
4545
{
4646
return $this->locations;
4747
}
48+
49+
public function setLocations($locations)
50+
{
51+
$this->locations = $locations;
52+
}
4853
}

0 commit comments

Comments
 (0)