Skip to content

Commit 0652580

Browse files
authored
Merge pull request #750 from soyuka/fix-many-to-many-eager
fix eager loading extension with many to many
2 parents 955d778 + ade3f76 commit 0652580

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ private function joinRelations(QueryBuilder $queryBuilder, string $resourceClass
8080
continue;
8181
}
8282

83-
$method = false === $mapping['joinColumns'][0]['nullable'] ? 'innerJoin' : 'leftJoin';
83+
$joinColumns = $mapping['joinColumns'] ?? $mapping['joinTable']['joinColumns'] ?? null;
84+
85+
if (null === $joinColumns) {
86+
$method = 'leftJoin';
87+
} else {
88+
$method = false === $joinColumns[0]['nullable'] ? 'innerJoin' : 'leftJoin';
89+
}
8490

8591
$associationAlias = $relationAlias.$i;
8692
$queryBuilder->{$method}($originAlias.'.'.$association, $associationAlias);

tests/Bridge/Doctrine/Orm/Extension/EagerLoadingExtensionTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public function testApplyToItem()
106106

107107
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy')->willReturn($relationPropertyMetadata)->shouldBeCalled();
108108
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy2')->willReturn($relationPropertyMetadata)->shouldBeCalled();
109+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy3')->willReturn($relationPropertyMetadata)->shouldBeCalled();
110+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy4')->willReturn($relationPropertyMetadata)->shouldBeCalled();
109111

110112
$idPropertyMetadata = new PropertyMetadata();
111113
$idPropertyMetadata = $idPropertyMetadata->withIdentifier(true);
@@ -124,10 +126,12 @@ public function testApplyToItem()
124126
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
125127

126128
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
127-
$classMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn([0 => 'relatedDummy', 'relatedDummy2']);
129+
$classMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn([0 => 'relatedDummy', 'relatedDummy2', 'relatedDummy3', 'relatedDummy4']);
128130
$classMetadataProphecy->associationMappings = [
129131
'relatedDummy' => ['fetch' => 3, 'joinColumns' => [['nullable' => true]], 'targetEntity' => DummyRelated::class],
130132
'relatedDummy2' => ['fetch' => 3, 'joinColumns' => [['nullable' => false]], 'targetEntity' => DummyRelated::class],
133+
'relatedDummy3' => ['fetch' => 3, 'joinTable' => ['joinColumns' => [['nullable' => false]]], 'targetEntity' => DummyRelated::class],
134+
'relatedDummy4' => ['fetch' => 3, 'targetEntity' => DummyRelated::class],
131135
];
132136

133137
$relatedClassMetadataProphecy = $this->prophesize(ClassMetadata::class);
@@ -146,8 +150,12 @@ public function testApplyToItem()
146150

147151
$queryBuilderProphecy->leftJoin('o.relatedDummy', 'a0')->shouldBeCalled(1);
148152
$queryBuilderProphecy->innerJoin('o.relatedDummy2', 'a11')->shouldBeCalled(1);
153+
$queryBuilderProphecy->innerJoin('o.relatedDummy3', 'a122')->shouldBeCalled(1);
154+
$queryBuilderProphecy->leftJoin('o.relatedDummy4', 'a1233')->shouldBeCalled(1);
149155
$queryBuilderProphecy->addSelect('partial a0.{id,name}')->shouldBeCalled(1);
150156
$queryBuilderProphecy->addSelect('partial a11.{id,name}')->shouldBeCalled(1);
157+
$queryBuilderProphecy->addSelect('partial a122.{id,name}')->shouldBeCalled(1);
158+
$queryBuilderProphecy->addSelect('partial a1233.{id,name}')->shouldBeCalled(1);
151159

152160
$em = $queryBuilderProphecy->getEntityManager()->shouldBeCalled(2)->willReturn($emProphecy->reveal());
153161

0 commit comments

Comments
 (0)