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