Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix changeset missing old value for exchanged associations #1437

Merged
merged 1 commit into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,11 @@ function ($assoc) { return empty($assoc['notSaved']); }
$oid2 = spl_object_hash($obj);

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

if ( ! $isNewDocument) {
$this->scheduleForUpdate($document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\ODM\MongoDB\Tests\Functional;

use Doctrine\Common\Collections\ArrayCollection;
use Documents\Bars\Bar;
use Documents\Bars\Location;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
Expand Down Expand Up @@ -65,6 +66,14 @@ public function testCollections()
$bar = $this->dm->find('Documents\Bars\Bar', $bar->getId());
$locations = $bar->getLocations();
$this->assertEquals(0, count($locations));
$this->dm->flush();

$bar->setLocations(new ArrayCollection([ new Location('Cracow') ]));
$this->uow->computeChangeSets();
$changeSet = $this->uow->getDocumentChangeSet($bar);
$this->assertNotEmpty($changeSet['locations']);
$this->assertSame($locations, $changeSet['locations'][0]);
$this->assertSame($bar->getLocations(), $changeSet['locations'][1]);
}

public function testCreateCollections()
Expand Down
10 changes: 10 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/EmbeddedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ public function testOneEmbedded()
->getSingleResult();
$this->assertNotNull($user);
$this->assertEquals($addressClone, $user->getAddress());

$oldAddress = $user->getAddress();
$address = new Address();
$address->setAddress('Someplace else');
$user->setAddress($address);
$this->uow->computeChangeSets();
$changeSet = $this->uow->getDocumentChangeSet($user);
$this->assertNotEmpty($changeSet['address']);
$this->assertSame($oldAddress, $changeSet['address'][0]);
$this->assertSame($user->getAddress(), $changeSet['address'][1]);
}

public function testRemoveOneEmbedded()
Expand Down
5 changes: 5 additions & 0 deletions tests/Documents/Bars/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public function getLocations()
{
return $this->locations;
}

public function setLocations($locations)
{
$this->locations = $locations;
}
}