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\Resource\Factory\ResourceMetadataFactoryInterface; |
16
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Fixes filters on OneToMany associations |
21
|
|
|
* https://github.com/api-platform/core/issues/944. |
22
|
|
|
*/ |
23
|
|
|
final class FilterEagerLoadingExtension implements QueryCollectionExtensionInterface |
24
|
|
|
{ |
25
|
|
|
private $resourceMetadataFactory; |
26
|
|
|
private $forceEager; |
27
|
|
|
|
28
|
|
|
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, $forceEager = true) |
29
|
|
|
{ |
30
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
31
|
|
|
$this->forceEager = $forceEager; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) |
38
|
|
|
{ |
39
|
|
|
if (false === $this->forceEager || false === $this->isForceEager($resourceClass, ['collection_operation_name' => $operationName])) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
//If no where part, nothing to do |
44
|
|
|
$wherePart = $queryBuilder->getDQLPart('where'); |
45
|
|
|
|
46
|
|
|
if (!$wherePart) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$joinParts = $queryBuilder->getDQLPart('join'); |
51
|
|
|
|
52
|
|
|
if (!$joinParts || !isset($joinParts['o'])) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$queryBuilderClone = clone $queryBuilder; |
57
|
|
|
$queryBuilderClone->resetDQLPart('where'); |
58
|
|
|
$queryBuilderClone->andWhere($queryBuilderClone->expr()->in('o', $this->getQueryBuilderWithNewAliases($queryBuilder, $queryNameGenerator)->getDQL())); |
59
|
|
|
|
60
|
|
|
$queryBuilder->resetDQLPart('where'); |
61
|
|
|
$queryBuilder->add('where', $queryBuilderClone->getDQLPart('where')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns a clone of the given query builder where everything gets re-aliased. |
66
|
|
|
* |
67
|
|
|
* @param QueryBuilder $queryBuilder |
68
|
|
|
* @param QueryNameGeneratorInterface $queryBuilder |
69
|
|
|
* @param string $originAlias - the base alias |
70
|
|
|
* @param string $replacement - the replacement for the base alias, will change the from alias |
71
|
|
|
*/ |
72
|
|
|
private function getQueryBuilderWithNewAliases(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $originAlias = 'o', string $replacement = 'o_2') |
73
|
|
|
{ |
74
|
|
|
$queryBuilderClone = clone $queryBuilder; |
75
|
|
|
$queryBuilderClone->select($replacement); |
76
|
|
|
|
77
|
|
|
$joinParts = $queryBuilder->getDQLPart('join'); |
78
|
|
|
$wherePart = $queryBuilder->getDQLPart('where'); |
79
|
|
|
|
80
|
|
|
//reset parts |
81
|
|
|
$queryBuilderClone->resetDQLPart('join'); |
82
|
|
|
$queryBuilderClone->resetDQLPart('where'); |
83
|
|
|
|
84
|
|
|
//Change from alias |
85
|
|
|
$from = $queryBuilderClone->getDQLPart('from')[0]; |
86
|
|
|
$queryBuilderClone->resetDQLPart('from'); |
87
|
|
|
$queryBuilderClone->from($from->getFrom(), $replacement); |
88
|
|
|
|
89
|
|
|
$aliases = ["$originAlias."]; |
90
|
|
|
$replacements = ["$replacement."]; |
91
|
|
|
|
92
|
|
|
//Change join aliases |
93
|
|
|
foreach ($joinParts[$originAlias] as $joinPart) { |
94
|
|
|
$aliases[] = "{$joinPart->getAlias()}."; |
95
|
|
|
$alias = $queryNameGenerator->generateJoinAlias($joinPart->getAlias()); |
96
|
|
|
$replacements[] = "$alias."; |
97
|
|
|
$join = new Join($joinPart->getJoinType(), str_replace($aliases, $replacements, $joinPart->getJoin()), $alias, $joinPart->getConditionType(), $joinPart->getCondition(), $joinPart->getIndexBy()); |
98
|
|
|
|
99
|
|
|
$queryBuilderClone->add('join', [$join], true); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$queryBuilderClone->add('where', str_replace($aliases, $replacements, (string) $wherePart)); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return $queryBuilderClone; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Does an operation force eager? |
109
|
|
|
* |
110
|
|
|
* @param string $resourceClass |
111
|
|
|
* @param array $options |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
private function isForceEager(string $resourceClass, array $options): bool |
116
|
|
|
{ |
117
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
118
|
|
|
|
119
|
|
|
if (isset($options['collection_operation_name'])) { |
120
|
|
|
$forceEager = $resourceMetadata->getCollectionOperationAttribute($options['collection_operation_name'], 'force_eager', null, true); |
121
|
|
|
} else { |
122
|
|
|
$forceEager = $resourceMetadata->getAttribute('force_eager'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return is_bool($forceEager) ? $forceEager : $this->forceEager; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: