diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index b6d509145b..3bb9b8efeb 100644 --- a/src/UnitOfWork.php +++ b/src/UnitOfWork.php @@ -3166,9 +3166,9 @@ public function createEntity($className, array $data, &$hints = []) if ($hints['fetchMode'][$class->name][$field] === ClassMetadata::FETCH_EAGER) { $isIteration = isset($hints[Query::HINT_INTERNAL_ITERATION]) && $hints[Query::HINT_INTERNAL_ITERATION]; - if (! $isIteration && $assoc['type'] === ClassMetadata::ONE_TO_MANY) { + if ($assoc['type'] === ClassMetadata::ONE_TO_MANY && ! $isIteration && ! $targetClass->isIdentifierComposite) { $this->scheduleCollectionForBatchLoading($pColl, $class); - } elseif (($isIteration && $assoc['type'] === ClassMetadata::ONE_TO_MANY) || $assoc['type'] === ClassMetadata::MANY_TO_MANY) { + } else { $this->loadCollection($pColl); $pColl->takeSnapshot(); } diff --git a/tests/Tests/Models/EagerFetchedCompositeOneToMany/RootEntity.php b/tests/Tests/Models/EagerFetchedCompositeOneToMany/RootEntity.php new file mode 100644 index 0000000000..af16c68697 --- /dev/null +++ b/tests/Tests/Models/EagerFetchedCompositeOneToMany/RootEntity.php @@ -0,0 +1,56 @@ + + */ + private $secondLevel; + + public function __construct(int $id, string $other) + { + $this->otherKey = $other; + $this->secondLevel = new ArrayCollection(); + $this->id = $id; + } + + public function getId(): ?int + { + return $this->id; + } + + public function getOtherKey(): string + { + return $this->otherKey; + } +} diff --git a/tests/Tests/Models/EagerFetchedCompositeOneToMany/SecondLevel.php b/tests/Tests/Models/EagerFetchedCompositeOneToMany/SecondLevel.php new file mode 100644 index 0000000000..33f878c8f4 --- /dev/null +++ b/tests/Tests/Models/EagerFetchedCompositeOneToMany/SecondLevel.php @@ -0,0 +1,55 @@ +upperId = $upper->getId(); + $this->otherKey = $upper->getOtherKey(); + $this->root = $upper; + } + + public function getId(): ?int + { + return $this->id; + } +} diff --git a/tests/Tests/ORM/Functional/EagerFetchOneToManyWithCompositeKeyTest.php b/tests/Tests/ORM/Functional/EagerFetchOneToManyWithCompositeKeyTest.php new file mode 100644 index 0000000000..82b9d0b8ac --- /dev/null +++ b/tests/Tests/ORM/Functional/EagerFetchOneToManyWithCompositeKeyTest.php @@ -0,0 +1,27 @@ +setUpEntitySchema([RootEntity::class, SecondLevel::class]); + + $a1 = new RootEntity(1, 'A'); + + $this->_em->persist($a1); + $this->_em->flush(); + + $this->_em->clear(); + + self::assertCount(1, $this->_em->getRepository(RootEntity::class)->findAll()); + } +}