From 96d13ac62adf7169e36189a8547280dd400604d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Mon, 3 Jun 2024 12:11:22 +0100 Subject: [PATCH] Fetching entities with Composite Key Relations and null values Remove redundant condition to check if target class contains foreign identifier in order to allow fetching a null for relations with composite keys, when part of the key value is null. --- src/UnitOfWork.php | 5 +- .../CompositeKeyRelations/CustomerClass.php | 33 ++++++++++ .../CompositeKeyRelations/InvoiceClass.php | 46 ++++++++++++++ .../Functional/CompositeKeyRelationsTest.php | 61 +++++++++++++++++++ tests/Tests/OrmFunctionalTestCase.php | 4 ++ 5 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 tests/Tests/Models/CompositeKeyRelations/CustomerClass.php create mode 100644 tests/Tests/Models/CompositeKeyRelations/InvoiceClass.php create mode 100644 tests/Tests/ORM/Functional/CompositeKeyRelationsTest.php diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index f1affcf7ebc..39ba6b68b7f 100644 --- a/src/UnitOfWork.php +++ b/src/UnitOfWork.php @@ -3053,10 +3053,7 @@ public function createEntity($className, array $data, &$hints = []) } else { $associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue; } - } elseif ( - $targetClass->containsForeignIdentifier - && in_array($targetClass->getFieldForColumn($targetColumn), $targetClass->identifier, true) - ) { + } elseif (in_array($targetClass->getFieldForColumn($targetColumn), $targetClass->identifier, true)) { // the missing key is part of target's entity primary key $associatedId = []; break; diff --git a/tests/Tests/Models/CompositeKeyRelations/CustomerClass.php b/tests/Tests/Models/CompositeKeyRelations/CustomerClass.php new file mode 100644 index 00000000000..aca13e4c8ee --- /dev/null +++ b/tests/Tests/Models/CompositeKeyRelations/CustomerClass.php @@ -0,0 +1,33 @@ +useModelSet('compositekeyrelations'); + + parent::setUp(); + } + + public function testFindEntityWithNotNullRelation(): void + { + $this->_em->getConnection()->insert('CustomerClass', [ + 'companyCode' => 'AA', + 'code' => 'CUST1', + 'name' => 'Customer 1', + ]); + + $this->_em->getConnection()->insert('InvoiceClass', [ + 'companyCode' => 'AA', + 'invoiceNumber' => 'INV1', + 'customerCode' => 'CUST1', + ]); + + $entity = $this->findEntity('AA', 'INV1'); + self::assertSame('AA', $entity->companyCode); + self::assertSame('INV1', $entity->invoiceNumber); + self::assertInstanceOf(CustomerClass::class, $entity->customer); + self::assertSame('Customer 1', $entity->customer->name); + } + + public function testFindEntityWithNullRelation(): void + { + $this->_em->getConnection()->insert('InvoiceClass', [ + 'companyCode' => 'BB', + 'invoiceNumber' => 'INV1', + ]); + + $entity = $this->findEntity('BB', 'INV1'); + self::assertSame('BB', $entity->companyCode); + self::assertSame('INV1', $entity->invoiceNumber); + self::assertNull($entity->customer); + } + + private function findEntity(string $companyCode, string $invoiceNumber): InvoiceClass + { + return $this->_em->find( + InvoiceClass::class, + ['companyCode' => $companyCode, 'invoiceNumber' => $invoiceNumber] + ); + } +} diff --git a/tests/Tests/OrmFunctionalTestCase.php b/tests/Tests/OrmFunctionalTestCase.php index 4a64136f184..f81eddc7d59 100644 --- a/tests/Tests/OrmFunctionalTestCase.php +++ b/tests/Tests/OrmFunctionalTestCase.php @@ -212,6 +212,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase Models\CompositeKeyInheritance\SingleRootClass::class, Models\CompositeKeyInheritance\SingleChildClass::class, ], + 'compositekeyrelations' => [ + Models\CompositeKeyRelations\InvoiceClass::class, + Models\CompositeKeyRelations\CustomerClass::class, + ], 'taxi' => [ Models\Taxi\PaidRide::class, Models\Taxi\Ride::class,