Skip to content

Commit 23b74e4

Browse files
authored
Merge pull request #12063 from wmouwen/test/gh-10788
Proxy class with BackedEnum as primary key does not convert the enum
2 parents 2c01dac + d2b699e commit 23b74e4

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

src/Persisters/Entity/BasicEntityPersister.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,11 @@ protected function prepareUpdateData(object $entity, bool $isInsert = false): ar
693693
$targetColumn = $joinColumn->referencedColumnName;
694694
$quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform);
695695

696-
$this->quotedColumns[$sourceColumn] = $quotedColumn;
697-
$this->columnTypes[$sourceColumn] = PersisterHelper::getTypeOfColumn($targetColumn, $targetClass, $this->em);
698-
$result[$owningTable][$sourceColumn] = $newValId
699-
? $newValId[$targetClass->getFieldForColumn($targetColumn)]
700-
: null;
696+
$this->quotedColumns[$sourceColumn] = $quotedColumn;
697+
$this->columnTypes[$sourceColumn] = PersisterHelper::getTypeOfColumn($targetColumn, $targetClass, $this->em);
698+
699+
$newValue = $newValId ? $newValId[$targetClass->getFieldForColumn($targetColumn)] : null;
700+
$result[$owningTable][$sourceColumn] = $newValue instanceof BackedEnum ? $newValue->value : $newValue;
701701
}
702702
}
703703

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional\Ticket;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Doctrine\ORM\Mapping\Column;
9+
use Doctrine\ORM\Mapping\Entity;
10+
use Doctrine\ORM\Mapping\GeneratedValue;
11+
use Doctrine\ORM\Mapping\Id;
12+
use Doctrine\Tests\OrmFunctionalTestCase;
13+
14+
class GH12063 extends OrmFunctionalTestCase
15+
{
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->createSchemaForModels(GH12063Association::class, GH12063Entity::class);
21+
}
22+
23+
public function testLoadedAssociationWithBackedEnum(): void
24+
{
25+
$association = new GH12063Association();
26+
$association->code = GH12063Code::One;
27+
28+
$this->_em->persist($association);
29+
$this->_em->flush();
30+
$this->_em->clear();
31+
32+
$entity = new GH12063Entity();
33+
$entity->association = $this->_em->find(GH12063Association::class, GH12063Code::One);
34+
35+
$this->_em->persist($entity);
36+
$this->_em->flush();
37+
38+
$this->assertNotNull($entity->id);
39+
}
40+
41+
public function testProxyAssociationWithBackedEnum(): void
42+
{
43+
$association = new GH12063Association();
44+
$association->code = GH12063Code::Two;
45+
46+
$this->_em->persist($association);
47+
$this->_em->flush();
48+
$this->_em->clear();
49+
50+
$entity = new GH12063Entity();
51+
$entity->association = $this->_em->getReference(GH12063Association::class, GH12063Code::Two);
52+
53+
$this->_em->persist($entity);
54+
$this->_em->flush();
55+
56+
$this->assertNotNull($entity->id);
57+
}
58+
}
59+
60+
enum GH12063Code: string
61+
{
62+
case One = 'one';
63+
case Two = 'two';
64+
}
65+
66+
#[Entity]
67+
class GH12063Association
68+
{
69+
#[Id]
70+
#[Column]
71+
public GH12063Code $code;
72+
}
73+
74+
#[Entity]
75+
class GH12063Entity
76+
{
77+
#[Id]
78+
#[Column]
79+
#[GeneratedValue]
80+
public int|null $id = null;
81+
82+
#[ORM\ManyToOne]
83+
#[ORM\JoinColumn(referencedColumnName: 'code')]
84+
public GH12063Association $association;
85+
}

0 commit comments

Comments
 (0)