Skip to content

Commit 457ffac

Browse files
committed
fix: Do not change readonly id when deleting (#9538)
1 parent 580a95c commit 457ffac

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/UnitOfWork.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Doctrine\ORM\Mapping\AssociationMapping;
3636
use Doctrine\ORM\Mapping\ClassMetadata;
3737
use Doctrine\ORM\Mapping\MappingException;
38+
use Doctrine\ORM\Mapping\PropertyAccessors\ReadonlyAccessor;
3839
use Doctrine\ORM\Mapping\ToManyInverseSideMapping;
3940
use Doctrine\ORM\Persisters\Collection\CollectionPersister;
4041
use Doctrine\ORM\Persisters\Collection\ManyToManyPersister;
@@ -1176,7 +1177,11 @@ private function executeDeletions(): void
11761177
// Entity with this $oid after deletion treated as NEW, even if the $oid
11771178
// is obtained by a new entity because the old one went out of scope.
11781179
//$this->entityStates[$oid] = self::STATE_NEW;
1179-
if (! $class->isIdentifierNatural()) {
1180+
if (
1181+
! $class->isIdentifierNatural() && ! (
1182+
$class->propertyAccessors[$class->identifier[0]] instanceof ReadonlyAccessor &&
1183+
$class->propertyAccessors[$class->identifier[0]]->getUnderlyingReflector()->isInitialized($entity))
1184+
) {
11801185
$class->propertyAccessors[$class->identifier[0]]->setValue($entity, null);
11811186
}
11821187

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional\Ticket;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Entity;
9+
use Doctrine\ORM\Mapping\GeneratedValue;
10+
use Doctrine\ORM\Mapping\Id;
11+
use Doctrine\Tests\OrmFunctionalTestCase;
12+
13+
class GH9538Test extends OrmFunctionalTestCase
14+
{
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->setUpEntitySchema([
20+
GH9538EntityA::class,
21+
]);
22+
}
23+
24+
public function testCanRemoveEntityWithReadonlyId(): void
25+
{
26+
$this->_em->persist($entity = new GH9538EntityA());
27+
$this->_em->flush();
28+
$this->_em->remove($entity);
29+
$this->_em->flush();
30+
$this->expectNotToPerformAssertions();
31+
}
32+
}
33+
34+
#[Entity]
35+
class GH9538EntityA
36+
{
37+
#[Column(type: 'integer')]
38+
#[Id]
39+
#[GeneratedValue(strategy: 'AUTO')]
40+
public readonly int $id;
41+
}

0 commit comments

Comments
 (0)