Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,7 @@ private function executeDeletions(): void
$this->entityIdentifiers[$oid],
$this->originalEntityData[$oid],
$this->entityStates[$oid],
$this->partialObjectLoadedFields[$oid],
);

// Entity with this $oid after deletion treated as NEW, even if the $oid
Expand Down Expand Up @@ -2131,6 +2132,7 @@ private function doDetach(
$this->entityIdentifiers[$oid],
$this->entityStates[$oid],
$this->originalEntityData[$oid],
$this->partialObjectLoadedFields[$oid],
);
break;
case self::STATE_NEW:
Expand Down
102 changes: 102 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH12784Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;

use function spl_object_id;

/** @see https://github.com/doctrine/orm/issues/12784 */
#[RequiresPhp('>=8.4.0')]
class GH12784Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

if (! $this->_em->getConfiguration()->isNativeLazyObjectsEnabled()) {
$this->markTestSkipped('Test requires native lazy objects enabled.');
}

$this->createSchemaForModels(GH12784Entity::class);
}

public function testDetachedProxyDoesNotLeakPartialLoadStateToReusedObjectId(): void
{
$id = $this->createRow();

$proxy = $this->_em->getReference(GH12784Entity::class, $id);
$objectId = spl_object_id($proxy);

$this->_em->detach($proxy);
unset($proxy);

$this->assertNoUpdateForNewEntityWithObjectId($objectId);
}

public function testRemovedProxyDoesNotLeakPartialLoadStateToReusedObjectId(): void
{
$id = $this->createRow();

$proxy = $this->_em->getReference(GH12784Entity::class, $id);
$objectId = spl_object_id($proxy);

$this->_em->remove($proxy);
$this->_em->flush();
unset($proxy);

$this->assertNoUpdateForNewEntityWithObjectId($objectId);
}

private function createRow(): int
{
$entity = new GH12784Entity();
$this->_em->persist($entity);
$this->_em->flush();
$this->_em->clear();

return $entity->id;
}

private function assertNoUpdateForNewEntityWithObjectId(int $objectId): void
{
$held = [];

for ($i = 0; $i < 1000; $i++) {
$entity = new GH12784Entity();

if (spl_object_id($entity) === $objectId) {
break;
}

$held[] = $entity;
}

self::assertSame($objectId, spl_object_id($entity), 'Could not reuse the object id of the released proxy.');

$this->_em->persist($entity);
$this->_em->flush();

$this->_em->getUnitOfWork()->computeChangeSets();

self::assertSame([], $this->_em->getUnitOfWork()->getEntityChangeSet($entity));
self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($entity));
}
}

#[ORM\Entity]
#[ORM\Table(name: 'gh12784_entity')]
class GH12784Entity
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
public int $id;

#[ORM\Column]
public string $name = 'new';
}
Loading