1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Tools\Pagination; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Types\Type; |
8
|
|
|
use Doctrine\ORM\Mapping\AssociationMetadata; |
9
|
|
|
use Doctrine\ORM\Mapping\ToManyAssociationMetadata; |
10
|
|
|
use Doctrine\ORM\Query; |
11
|
|
|
use Doctrine\ORM\Query\AST\Functions\IdentityFunction; |
12
|
|
|
use Doctrine\ORM\Query\AST\PathExpression; |
13
|
|
|
use Doctrine\ORM\Query\AST\SelectExpression; |
14
|
|
|
use Doctrine\ORM\Query\AST\SelectStatement; |
15
|
|
|
use Doctrine\ORM\Query\TreeWalkerAdapter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Replaces the selectClause of the AST with a SELECT DISTINCT root.id equivalent. |
19
|
|
|
*/ |
20
|
|
|
class LimitSubqueryWalker extends TreeWalkerAdapter |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* ID type hint. |
24
|
|
|
*/ |
25
|
|
|
public const IDENTIFIER_TYPE = 'doctrine_paginator.id.type'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Counter for generating unique order column aliases. |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $aliasCounter = 0; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Walks down a SelectStatement AST node, modifying it to retrieve DISTINCT ids |
36
|
|
|
* of the root Entity. |
37
|
|
|
* |
38
|
|
|
* @throws \RuntimeException |
39
|
|
|
*/ |
40
|
33 |
|
public function walkSelectStatement(SelectStatement $AST) |
41
|
|
|
{ |
42
|
33 |
|
$queryComponents = $this->getQueryComponents(); |
43
|
|
|
// Get the root entity and alias from the AST fromClause |
44
|
33 |
|
$from = $AST->fromClause->identificationVariableDeclarations; |
45
|
33 |
|
$fromRoot = reset($from); |
46
|
33 |
|
$rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; |
47
|
33 |
|
$rootClass = $queryComponents[$rootAlias]['metadata']; |
48
|
|
|
|
49
|
33 |
|
$this->validate($AST); |
50
|
31 |
|
$property = $rootClass->getProperty($rootClass->getSingleIdentifierFieldName()); |
51
|
|
|
|
52
|
31 |
|
if ($property instanceof AssociationMetadata) { |
53
|
1 |
|
throw new \RuntimeException( |
54
|
|
|
'Paginating an entity with foreign key as identifier only works when using the Output Walkers. ' . |
55
|
1 |
|
'Call Paginator#setUseOutputWalkers(true) before iterating the paginator.' |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
30 |
|
$this->getQuery()->setHint(self::IDENTIFIER_TYPE, $property->getType()); |
60
|
|
|
|
61
|
30 |
|
$pathExpression = new PathExpression( |
62
|
30 |
|
PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, |
63
|
30 |
|
$rootAlias, |
64
|
30 |
|
$property->getName() |
65
|
|
|
); |
66
|
|
|
|
67
|
30 |
|
$pathExpression->type = PathExpression::TYPE_STATE_FIELD; |
68
|
|
|
|
69
|
30 |
|
$AST->selectClause->selectExpressions = [new SelectExpression($pathExpression, '_dctrn_id')]; |
70
|
30 |
|
$AST->selectClause->isDistinct = true; |
71
|
|
|
|
72
|
30 |
|
if (! isset($AST->orderByClause)) { |
73
|
6 |
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
24 |
|
foreach ($AST->orderByClause->orderByItems as $item) { |
77
|
24 |
|
if ($item->expression instanceof PathExpression) { |
78
|
20 |
|
$AST->selectClause->selectExpressions[] = new SelectExpression( |
79
|
20 |
|
$this->createSelectExpressionItem($item->expression), |
80
|
20 |
|
'_dctrn_ord' . $this->aliasCounter++ |
81
|
|
|
); |
82
|
|
|
|
83
|
20 |
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
if (is_string($item->expression) && isset($queryComponents[$item->expression])) { |
87
|
3 |
|
$qComp = $queryComponents[$item->expression]; |
88
|
|
|
|
89
|
3 |
|
if (isset($qComp['resultVariable'])) { |
90
|
3 |
|
$AST->selectClause->selectExpressions[] = new SelectExpression( |
91
|
3 |
|
$qComp['resultVariable'], |
92
|
4 |
|
$item->expression |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
24 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Validate the AST to ensure that this walker is able to properly manipulate it. |
101
|
|
|
*/ |
102
|
33 |
|
private function validate(SelectStatement $AST) |
103
|
|
|
{ |
104
|
|
|
// Prevent LimitSubqueryWalker from being used with queries that include |
105
|
|
|
// a limit, a fetched to-many join, and an order by condition that |
106
|
|
|
// references a column from the fetch joined table. |
107
|
33 |
|
$queryComponents = $this->getQueryComponents(); |
108
|
33 |
|
$query = $this->getQuery(); |
109
|
33 |
|
$from = $AST->fromClause->identificationVariableDeclarations; |
110
|
33 |
|
$fromRoot = reset($from); |
111
|
|
|
|
112
|
33 |
|
if ($query instanceof Query && $query->getMaxResults() && $AST->orderByClause && count($fromRoot->joins)) { |
|
|
|
|
113
|
|
|
// Check each orderby item. |
114
|
|
|
// TODO: check complex orderby items too... |
115
|
10 |
|
foreach ($AST->orderByClause->orderByItems as $orderByItem) { |
116
|
10 |
|
$expression = $orderByItem->expression; |
117
|
|
|
|
118
|
10 |
|
if ($expression instanceof PathExpression && isset($queryComponents[$expression->identificationVariable])) { |
119
|
10 |
|
$queryComponent = $queryComponents[$expression->identificationVariable]; |
120
|
|
|
|
121
|
10 |
|
if (isset($queryComponent['parent']) && $queryComponent['relation'] instanceof ToManyAssociationMetadata) { |
122
|
2 |
|
throw new \RuntimeException( |
123
|
|
|
'Cannot select distinct identifiers from query with LIMIT and ORDER BY on a column from a ' |
124
|
10 |
|
. 'fetch joined to-many association. Use output walkers.' |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
31 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Retrieve either an IdentityFunction (IDENTITY(u.assoc)) or a state field (u.name). |
134
|
|
|
* |
135
|
|
|
* @return \Doctrine\ORM\Query\AST\Functions\IdentityFunction |
136
|
|
|
*/ |
137
|
20 |
|
private function createSelectExpressionItem(PathExpression $pathExpression) |
138
|
|
|
{ |
139
|
20 |
|
if ($pathExpression->type === PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION) { |
140
|
1 |
|
$identity = new IdentityFunction('identity'); |
141
|
|
|
|
142
|
1 |
|
$identity->pathExpression = clone $pathExpression; |
143
|
|
|
|
144
|
1 |
|
return $identity; |
145
|
|
|
} |
146
|
|
|
|
147
|
19 |
|
return clone $pathExpression; |
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: