From 66a95ea7f919876cab798c76e277c27d184a9ddf Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Tue, 8 Dec 2020 03:21:55 +0100 Subject: [PATCH] [GH-6394] Bugfix: IdentifierFlattener support for association non-object values --- .../ORM/Utility/IdentifierFlattener.php | 6 +- .../ORM/Functional/Ticket/GH6394Test.php | 99 +++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/GH6394Test.php diff --git a/lib/Doctrine/ORM/Utility/IdentifierFlattener.php b/lib/Doctrine/ORM/Utility/IdentifierFlattener.php index 992e8540e6f..3acbc9b9c77 100644 --- a/lib/Doctrine/ORM/Utility/IdentifierFlattener.php +++ b/lib/Doctrine/ORM/Utility/IdentifierFlattener.php @@ -71,13 +71,15 @@ public function flattenIdentifier(ClassMetadata $class, array $id) $flatId = []; foreach ($class->identifier as $field) { - if (isset($class->associationMappings[$field]) && isset($id[$field]) && is_object($id[$field])) { + if (isset($class->associationMappings[$field]) && isset($id[$field])) { /* @var $targetClassMetadata ClassMetadata */ $targetClassMetadata = $this->metadataFactory->getMetadataFor( $class->associationMappings[$field]['targetEntity'] ); - if ($this->unitOfWork->isInIdentityMap($id[$field])) { + if (!is_object($id[$field])) { + $associatedId = [$id[$field]]; + } elseif ($this->unitOfWork->isInIdentityMap($id[$field])) { $associatedId = $this->flattenIdentifier($targetClassMetadata, $this->unitOfWork->getEntityIdentifier($id[$field])); } else { $associatedId = $this->flattenIdentifier($targetClassMetadata, $targetClassMetadata->getIdentifierValues($id[$field])); 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..d78ded4370d --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6394Test.php @@ -0,0 +1,99 @@ +_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") + */ + public $id; + + /** + * @Version + * @Column(type="integer") + */ + public $version; + + public function __construct(int $id) + { + $this->id = $id; + } + +} + +/** + * @Entity + */ +class B +{ + + /** + * @Id + * @ManyToOne(targetEntity="A") + * @JoinColumn(name="aid", referencedColumnName="id") + */ + public $a; + + /** + * @Column(type="string") + */ + public $something; + + /** + * @Version + * @Column(type="integer") + */ + public $version; + + public function __construct(A $a, string $something) + { + $this->a = $a; + $this->something = $something; + } + +} \ No newline at end of file