Skip to content

Commit

Permalink
Merge pull request #2108 from alcaeus/gh-2106
Browse files Browse the repository at this point in the history
Fix merging of documents when not cascading references
  • Loading branch information
alcaeus committed Nov 13, 2019
2 parents 182a04b + 03eadf7 commit 8cf8fa0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,11 @@ private function doMerge($document, array &$visited, $prevManagedCopy = null, $a
$targetClass = $this->dm->getClassMetadata($targetDocument);
$relatedId = $targetClass->getIdentifierObject($other);

$current = $prop->getValue($managedCopy);
if ($current !== null) {
$this->removeFromIdentityMap($current);
}

if ($targetClass->subClasses) {
$other = $this->dm->find($targetClass->name, $relatedId);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests\ORM\Functional;

use Documents\CmsArticle;
use Documents\CmsUser;
use Documents\CmsPhonenumber;
use Documents\CmsAddress;
Expand Down Expand Up @@ -132,4 +133,28 @@ public function testUninitializedLazyAssociationsAreIgnoredOnMerge()
$this->assertNotSame($managedAddress2->user, $detachedAddress2->user);
$this->assertFalse($managedAddress2->user->__isInitialized());
}

public function testMergeWithReference()
{
$cmsUser = new CmsUser();
$cmsUser->username = 'alcaeus';

$cmsArticle = new CmsArticle();
$cmsArticle->setAuthor($cmsUser);

$this->dm->persist($cmsUser);
$this->dm->persist($cmsArticle);
$this->dm->flush();
$this->dm->clear();

/** @var CmsArticle $cmsArticle */
$cmsArticle = $this->dm->find(CmsArticle::class, $cmsArticle->id);
$this->assertInstanceOf(CmsArticle::class, $cmsArticle);
$this->assertSame('alcaeus', $cmsArticle->user->getUsername());
$this->dm->clear();

$cmsArticle = $this->dm->merge($cmsArticle);

$this->assertSame('alcaeus', $cmsArticle->user->getUsername());
}
}

0 comments on commit 8cf8fa0

Please sign in to comment.