|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the API Platform project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
|
15
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
|
16
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
|
17
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
18
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Eager loads relations. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Charles Sarrazin <[email protected]> |
|
24
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
25
|
|
|
* @author Antoine Bluchet <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class EagerLoadingExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface |
|
28
|
|
|
{ |
|
29
|
|
|
private $propertyNameCollectionFactory; |
|
30
|
|
|
private $propertyMetadataFactory; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->propertyMetadataFactory = $propertyMetadataFactory; |
|
35
|
|
|
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
View Code Duplication |
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
if (null !== $operationName) { |
|
44
|
|
|
$propertyMetadataOptions = ['collection_operation_name' => $operationName]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->joinRelations($queryBuilder, $resourceClass, $propertyMetadataOptions ?? []); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
View Code Duplication |
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
if (null !== $operationName) { |
|
56
|
|
|
$propertyMetadataOptions = ['item_operation_name' => $operationName]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->joinRelations($queryBuilder, $resourceClass, $propertyMetadataOptions ?? []); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Left joins relations to eager load. |
|
64
|
|
|
* |
|
65
|
|
|
* @param QueryBuilder $queryBuilder |
|
66
|
|
|
* @param string $resourceClass |
|
67
|
|
|
* @param array $propertyMetadataOptions |
|
68
|
|
|
* @param string $originAlias the current entity alias (first o, then a1, a2 etc.) |
|
69
|
|
|
* @param string $relationAlias the previous relation alias to keep it unique |
|
70
|
|
|
* @param bool $wasLeftJoin if the relation containing the new one had a left join, we have to force the new one to left join too |
|
71
|
|
|
*/ |
|
72
|
|
|
private function joinRelations(QueryBuilder $queryBuilder, string $resourceClass, array $propertyMetadataOptions = [], string $originAlias = 'o', string &$relationAlias = 'a', bool $wasLeftJoin = false) |
|
73
|
|
|
{ |
|
74
|
|
|
$entityManager = $queryBuilder->getEntityManager(); |
|
75
|
|
|
$classMetadata = $entityManager->getClassMetadata($resourceClass); |
|
76
|
|
|
$j = 0; |
|
77
|
|
|
|
|
78
|
|
|
foreach ($classMetadata->getAssociationNames() as $i => $association) { |
|
79
|
|
|
$mapping = $classMetadata->associationMappings[$association]; |
|
80
|
|
|
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $propertyMetadataOptions); |
|
81
|
|
|
|
|
82
|
|
|
if (ClassMetadataInfo::FETCH_EAGER !== $mapping['fetch'] || false === $propertyMetadata->isReadableLink()) { |
|
83
|
|
|
continue; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (false === $wasLeftJoin) { |
|
87
|
|
|
$joinColumns = $mapping['joinColumns'] ?? $mapping['joinTable']['joinColumns'] ?? null; |
|
88
|
|
|
|
|
89
|
|
|
if (null === $joinColumns) { |
|
90
|
|
|
$method = 'leftJoin'; |
|
91
|
|
|
} else { |
|
92
|
|
|
$method = false === $joinColumns[0]['nullable'] ? 'innerJoin' : 'leftJoin'; |
|
93
|
|
|
} |
|
94
|
|
|
} else { |
|
95
|
|
|
$method = 'leftJoin'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$associationAlias = $relationAlias.$i; |
|
99
|
|
|
$queryBuilder->{$method}($originAlias.'.'.$association, $associationAlias); |
|
100
|
|
|
$select = []; |
|
101
|
|
|
$targetClassMetadata = $entityManager->getClassMetadata($mapping['targetEntity']); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($this->propertyNameCollectionFactory->create($mapping['targetEntity']) as $property) { |
|
104
|
|
|
$propertyMetadata = $this->propertyMetadataFactory->create($mapping['targetEntity'], $property, $propertyMetadataOptions); |
|
105
|
|
|
|
|
106
|
|
|
if (true === $propertyMetadata->isIdentifier()) { |
|
107
|
|
|
$select[] = $property; |
|
108
|
|
|
continue; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
//the field test allows to add methods to a Resource which do not reflect real database fields |
|
112
|
|
|
if (true === $targetClassMetadata->hasField($property) && true === $propertyMetadata->isReadable()) { |
|
113
|
|
|
$select[] = $property; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$queryBuilder->addSelect(sprintf('partial %s.{%s}', $associationAlias, implode(',', $select))); |
|
118
|
|
|
|
|
119
|
|
|
$relationAlias = $relationAlias.++$j; |
|
120
|
|
|
|
|
121
|
|
|
$this->joinRelations($queryBuilder, $mapping['targetEntity'], $propertyMetadataOptions, $associationAlias, $relationAlias, $method === 'leftJoin'); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.