|
14 | 14 | namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Extension; |
15 | 15 |
|
16 | 16 | use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\EagerLoadingTrait; |
| 17 | +use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryBuilderHelper; |
17 | 18 | use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
18 | 19 | use ApiPlatform\Core\Exception\InvalidArgumentException; |
19 | 20 | use ApiPlatform\Core\Exception\PropertyNotFoundException; |
|
24 | 25 | use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
25 | 26 | use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; |
26 | 27 | use Doctrine\ORM\Mapping\ClassMetadataInfo; |
| 28 | +use Doctrine\ORM\Query\Expr\Join; |
27 | 29 | use Doctrine\ORM\QueryBuilder; |
28 | 30 | use Symfony\Component\HttpFoundation\RequestStack; |
29 | 31 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
@@ -190,16 +192,20 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt |
190 | 192 | continue; |
191 | 193 | } |
192 | 194 |
|
193 | | - $isNullable = $mapping['joinColumns'][0]['nullable'] ?? true; |
194 | | - if (false !== $wasLeftJoin || true === $isNullable) { |
195 | | - $method = 'leftJoin'; |
| 195 | + $existingJoin = QueryBuilderHelper::getExistingJoin($queryBuilder, $parentAlias, $association); |
| 196 | + |
| 197 | + if (null !== $existingJoin) { |
| 198 | + $associationAlias = $existingJoin->getAlias(); |
| 199 | + $isLeftJoin = Join::LEFT_JOIN === $existingJoin->getJoinType(); |
196 | 200 | } else { |
197 | | - $method = 'innerJoin'; |
198 | | - } |
| 201 | + $isNullable = $mapping['joinColumns'][0]['nullable'] ?? true; |
| 202 | + $isLeftJoin = false !== $wasLeftJoin || true === $isNullable; |
| 203 | + $method = $isLeftJoin ? 'leftJoin' : 'innerJoin'; |
199 | 204 |
|
200 | | - $associationAlias = $queryNameGenerator->generateJoinAlias($association); |
201 | | - $queryBuilder->{$method}(sprintf('%s.%s', $parentAlias, $association), $associationAlias); |
202 | | - ++$joinCount; |
| 205 | + $associationAlias = $queryNameGenerator->generateJoinAlias($association); |
| 206 | + $queryBuilder->{$method}(sprintf('%s.%s', $parentAlias, $association), $associationAlias); |
| 207 | + ++$joinCount; |
| 208 | + } |
203 | 209 |
|
204 | 210 | if (true === $fetchPartial) { |
205 | 211 | try { |
@@ -230,7 +236,7 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt |
230 | 236 | } |
231 | 237 | } |
232 | 238 |
|
233 | | - $this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $fetchPartial, $associationAlias, $options, $normalizationContext, 'leftJoin' === $method, $joinCount, $currentDepth); |
| 239 | + $this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $fetchPartial, $associationAlias, $options, $normalizationContext, $isLeftJoin, $joinCount, $currentDepth); |
234 | 240 | } |
235 | 241 | } |
236 | 242 |
|
|
0 commit comments