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

Avoid creating unmanaged proxy instances for referred-to entities during merge() #10791

Merged
merged 2 commits into from
Jun 25, 2023
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
20 changes: 12 additions & 8 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -3669,14 +3669,18 @@ private function mergeEntityStateIntoManagedCopy($entity, $managedCopy): void
$targetClass = $this->em->getClassMetadata($assoc2['targetEntity']);
$relatedId = $targetClass->getIdentifierValues($other);

if ($targetClass->subClasses) {
$other = $this->em->find($targetClass->name, $relatedId);
} else {
$other = $this->em->getProxyFactory()->getProxy(
$assoc2['targetEntity'],
$relatedId
);
$this->registerManaged($other, $relatedId, []);
$other = $this->tryGetById($relatedId, $targetClass->name);

if (! $other) {
if ($targetClass->subClasses) {
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
$other = $this->em->find($targetClass->name, $relatedId);
} else {
$other = $this->em->getProxyFactory()->getProxy(
$assoc2['targetEntity'],
$relatedId
);
$this->registerManaged($other, $relatedId, []);
}
}
}

Expand Down
88 changes: 88 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7407Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\UnitOfWork;
use Doctrine\Tests\Models\CMS\CmsArticle;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\OrmFunctionalTestCase;
use ReflectionClass;

use function spl_object_id;

class GH7407Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
$this->useModelSet('cms');

parent::setUp();
}

public function testMergingEntitiesDoesNotCreateUnmanagedProxyReferences(): void
{
// 1. Create an article with a user; persist, flush and clear the entity manager
$user = new CmsUser();
$user->username = 'Test';
$user->name = 'Test';
$this->_em->persist($user);

$article = new CmsArticle();
$article->topic = 'Test';
$article->text = 'Test';
$article->setAuthor($user);
$this->_em->persist($article);

$this->_em->flush();
$this->_em->clear();

// 2. Merge the user object back in:
// We get a new (different) entity object that represents the user instance
// which is now (through this object instance) managed by the EM/UoW
$mergedUser = $this->_em->merge($user);
$mergedUserOid = spl_object_id($mergedUser);

// 3. Merge the article object back in,
// the returned entity object is the article instance as it is managed by the EM/UoW
$mergedArticle = $this->_em->merge($article);
$mergedArticleOid = spl_object_id($mergedArticle);

self::assertSame($mergedUser, $mergedArticle->user, 'The $mergedArticle\'s #user property should hold the $mergedUser we obtained previously, since that\'s the only legitimate object instance representing the user from the UoW\'s point of view.');

// Inspect internal UoW state
$uow = $this->_em->getUnitOfWork();
$entityIdentifiers = $this->grabProperty('entityIdentifiers', $uow);
$identityMap = $this->grabProperty('identityMap', $uow);
$entityStates = $this->grabProperty('entityStates', $uow);

self::assertCount(2, $entityIdentifiers, 'UoW#entityIdentifiers contains exactly two OID -> ID value mapping entries one for the article, one for the user object');
self::assertArrayHasKey($mergedArticleOid, $entityIdentifiers);
self::assertArrayHasKey($mergedUserOid, $entityIdentifiers);

self::assertSame([
$mergedUserOid => UnitOfWork::STATE_MANAGED,
$mergedArticleOid => UnitOfWork::STATE_MANAGED,
], $entityStates, 'UoW#entityStates contains two OID -> state entries, one for the article, one for the user object');

self::assertCount(2, $entityIdentifiers);
self::assertArrayHasKey($mergedArticleOid, $entityIdentifiers);
self::assertArrayHasKey($mergedUserOid, $entityIdentifiers);

self::assertSame([
CmsUser::class => [$user->id => $mergedUser],
CmsArticle::class => [$article->id => $mergedArticle],
], $identityMap, 'The identity map contains exactly two objects, the article and the user.');
}

/** @return mixed */
private function grabProperty(string $name, UnitOfWork $uow)
mpdude marked this conversation as resolved.
Show resolved Hide resolved
{
$reflection = new ReflectionClass($uow);
$property = $reflection->getProperty($name);
$property->setAccessible(true);

return $property->getValue($uow);
}
}