diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index 0ccbb8ead5f..b6d509145bc 100644 --- a/src/UnitOfWork.php +++ b/src/UnitOfWork.php @@ -3249,7 +3249,19 @@ private function eagerLoadCollections(array $collections, array $mapping): void foreach ($found as $targetValue) { $sourceEntity = $targetProperty->getValue($targetValue); - $id = $this->identifierFlattener->flattenIdentifier($class, $class->getIdentifierValues($sourceEntity)); + if ($sourceEntity === null && isset($targetClass->associationMappings[$mappedBy]['joinColumns'])) { + // case where the hydration $targetValue itself has not yet fully completed, for example + // in case a bi-directional association is being hydrated and deferring eager loading is + // not possible due to subclassing. + $data = $this->getOriginalEntityData($targetValue); + $id = []; + foreach ($targetClass->associationMappings[$mappedBy]['joinColumns'] as $joinColumn) { + $id[] = $data[$joinColumn['name']]; + } + } else { + $id = $this->identifierFlattener->flattenIdentifier($class, $class->getIdentifierValues($sourceEntity)); + } + $idHash = implode(' ', $id); if (isset($mapping['indexBy'])) { diff --git a/tests/Tests/Models/AbstractFetchEager/AbstractRemoteControl.php b/tests/Tests/Models/AbstractFetchEager/AbstractRemoteControl.php new file mode 100644 index 00000000000..59d69da28a4 --- /dev/null +++ b/tests/Tests/Models/AbstractFetchEager/AbstractRemoteControl.php @@ -0,0 +1,50 @@ + + */ + public $users; + + public function __construct(string $name) + { + $this->name = $name; + $this->users = new ArrayCollection(); + } +} diff --git a/tests/Tests/Models/AbstractFetchEager/MobileRemoteControl.php b/tests/Tests/Models/AbstractFetchEager/MobileRemoteControl.php new file mode 100644 index 00000000000..50bdc25470b --- /dev/null +++ b/tests/Tests/Models/AbstractFetchEager/MobileRemoteControl.php @@ -0,0 +1,14 @@ +remoteControl = $control; + } +} diff --git a/tests/Tests/ORM/Functional/AbstractFetchEagerTest.php b/tests/Tests/ORM/Functional/AbstractFetchEagerTest.php new file mode 100644 index 00000000000..24e100a684c --- /dev/null +++ b/tests/Tests/ORM/Functional/AbstractFetchEagerTest.php @@ -0,0 +1,37 @@ +createSchemaForModels( + AbstractRemoteControl::class, + User::class + ); + + $control = new MobileRemoteControl('smart'); + $user = new User($control); + + $entityManage = $this->getEntityManager(); + + $entityManage->persist($control); + $entityManage->persist($user); + $entityManage->flush(); + $entityManage->clear(); + + $user = $entityManage->find(User::class, $user->id); + + self::assertNotNull($user); + self::assertEquals('smart', $user->remoteControl->name); + self::assertTrue($user->remoteControl->users->contains($user)); + } +}