diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index ff30f14f6d8..077fa26e5e5 100644 --- a/src/UnitOfWork.php +++ b/src/UnitOfWork.php @@ -61,6 +61,7 @@ use function array_sum; use function array_values; use function assert; +use function count; use function current; use function func_get_arg; use function func_num_args; @@ -3162,8 +3163,10 @@ public function createEntity($className, array $data, &$hints = []) $reflField->setValue($entity, $pColl); if ($hints['fetchMode'][$class->name][$field] === ClassMetadata::FETCH_EAGER) { - $isIteration = isset($hints[Query::HINT_INTERNAL_ITERATION]) && $hints[Query::HINT_INTERNAL_ITERATION]; - if ($assoc['type'] === ClassMetadata::ONE_TO_MANY && ! $isIteration && ! $targetClass->isIdentifierComposite && ! isset($assoc['indexBy'])) { + $isIteration = isset($hints[Query::HINT_INTERNAL_ITERATION]) && $hints[Query::HINT_INTERNAL_ITERATION]; + $isForeignKeyComposite = $targetClass->hasAssociation($assoc['mappedBy']) && count($targetClass->getAssociationMapping($assoc['mappedBy'])['joinColumns'] ?? []) > 1; + + if ($assoc['type'] === ClassMetadata::ONE_TO_MANY && ! $isIteration && ! $isForeignKeyComposite && ! isset($assoc['indexBy'])) { $this->scheduleCollectionForBatchLoading($pColl, $class); } else { $this->loadCollection($pColl); diff --git a/tests/Tests/Models/EagerFetchedCompositeOneToMany/RootEntity.php b/tests/Tests/Models/EagerFetchedCompositeOneToMany/RootEntity.php index af16c686970..c804c90369c 100644 --- a/tests/Tests/Models/EagerFetchedCompositeOneToMany/RootEntity.php +++ b/tests/Tests/Models/EagerFetchedCompositeOneToMany/RootEntity.php @@ -37,11 +37,19 @@ class RootEntity */ private $secondLevel; + /** + * @ORM\OneToMany(mappedBy="root", targetEntity=SecondLevelWithoutCompositePrimaryKey::class, fetch="EAGER") + * + * @var Collection + */ + private $anotherSecondLevel; + public function __construct(int $id, string $other) { - $this->otherKey = $other; - $this->secondLevel = new ArrayCollection(); - $this->id = $id; + $this->otherKey = $other; + $this->secondLevel = new ArrayCollection(); + $this->anotherSecondLevel = new ArrayCollection(); + $this->id = $id; } public function getId(): ?int diff --git a/tests/Tests/Models/EagerFetchedCompositeOneToMany/SecondLevelWithoutCompositePrimaryKey.php b/tests/Tests/Models/EagerFetchedCompositeOneToMany/SecondLevelWithoutCompositePrimaryKey.php new file mode 100644 index 00000000000..323ac52060a --- /dev/null +++ b/tests/Tests/Models/EagerFetchedCompositeOneToMany/SecondLevelWithoutCompositePrimaryKey.php @@ -0,0 +1,43 @@ +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 index 82b9d0b8acb..4a3645f5e3e 100644 --- a/tests/Tests/ORM/Functional/EagerFetchOneToManyWithCompositeKeyTest.php +++ b/tests/Tests/ORM/Functional/EagerFetchOneToManyWithCompositeKeyTest.php @@ -6,6 +6,7 @@ use Doctrine\Tests\Models\EagerFetchedCompositeOneToMany\RootEntity; use Doctrine\Tests\Models\EagerFetchedCompositeOneToMany\SecondLevel; +use Doctrine\Tests\Models\EagerFetchedCompositeOneToMany\SecondLevelWithoutCompositePrimaryKey; use Doctrine\Tests\OrmFunctionalTestCase; final class EagerFetchOneToManyWithCompositeKeyTest extends OrmFunctionalTestCase @@ -13,7 +14,7 @@ final class EagerFetchOneToManyWithCompositeKeyTest extends OrmFunctionalTestCas /** @ticket 11154 */ public function testItDoesNotThrowAnExceptionWhenTriggeringALoad(): void { - $this->setUpEntitySchema([RootEntity::class, SecondLevel::class]); + $this->setUpEntitySchema([RootEntity::class, SecondLevel::class, SecondLevelWithoutCompositePrimaryKey::class]); $a1 = new RootEntity(1, 'A');