From 657a30f8ceef2a78c2ff36a9fe6c6a6717c1c448 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Thu, 1 Apr 2021 23:16:53 +0200 Subject: [PATCH] [GH-6394] Bugfix: IdentifierFlattener support for association non-object values. (#8384) * [GH-6394] Bugfix: IdentifierFlattener support for association non-object values * [GH-6394] Bugfix: BasicEntityPersister::update used wrong identifiers for version assignment. * Exclude MissingNativeTypeHint phpcs rule as 7.4 is not lowest version. --- .../Entity/BasicEntityPersister.php | 2 +- phpcs.xml.dist | 1 + .../ORM/Functional/Ticket/GH6394Test.php | 103 ++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/GH6394Test.php diff --git a/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php index 9e17c7e214f..adbd5934b43 100644 --- a/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php @@ -400,7 +400,7 @@ public function update($entity) $this->updateTable($entity, $quotedTableName, $data, $isVersioned); if ($isVersioned) { - $id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); + $id = $this->class->getIdentifierValues($entity); $this->assignDefaultVersionValue($entity, $id); } diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 8b226ebb15a..821a7c499da 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -22,6 +22,7 @@ + diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6394Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6394Test.php new file mode 100644 index 00000000000..8a31c65d4c9 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6394Test.php @@ -0,0 +1,103 @@ +_schemaTool->createSchema( + [ + $this->_em->getClassMetadata(A::class), + $this->_em->getClassMetadata(B::class), + ] + ); + } + + /** + * Test the the version of an entity can be fetched, when the id field and + * the id column are different. + * + * @group 6393 + */ + public function testFetchVersionValueForDifferentIdFieldAndColumn(): void + { + $a = new A(1); + $this->_em->persist($a); + + $b = new B($a, 'foo'); + $this->_em->persist($b); + $this->_em->flush(); + + self::assertSame(1, $b->version); + + $b->something = 'bar'; + $this->_em->flush(); + + self::assertSame(2, $b->version); + } +} + +/** + * @Entity + */ +class A +{ + /** + * @Id + * @Column(type="integer") + * @var int + */ + public $id; + + /** + * @Version + * @Column(type="integer") + * @var int + */ + public $version; + + public function __construct(int $id) + { + $this->id = $id; + } +} + +/** + * @Entity + */ +class B +{ + /** + * @Id + * @ManyToOne(targetEntity="A") + * @JoinColumn(name="aid", referencedColumnName="id") + * @var A + */ + public $a; + + /** + * @Column(type="string") + * @var string + */ + public $something; + + /** + * @Version + * @Column(type="integer") + * @var int + */ + public $version; + + public function __construct(A $a, string $something) + { + $this->a = $a; + $this->something = $something; + } +}