1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ORM\Tools\Pagination; |
21
|
|
|
|
22
|
|
|
use Doctrine\ORM\NoResultException; |
23
|
|
|
use Doctrine\ORM\Query; |
24
|
|
|
use Doctrine\ORM\Query\Parser; |
25
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
26
|
|
|
use Doctrine\ORM\QueryBuilder; |
27
|
|
|
use function array_map; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The paginator can handle various complex scenarios with DQL. |
31
|
|
|
* |
32
|
|
|
* @author Pablo Díez <[email protected]> |
33
|
|
|
* @author Benjamin Eberlei <[email protected]> |
34
|
|
|
* @license New BSD |
35
|
|
|
*/ |
36
|
|
|
class Paginator implements \Countable, \IteratorAggregate |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var Query |
40
|
|
|
*/ |
41
|
|
|
private $query; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
private $fetchJoinCollection; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool|null |
50
|
|
|
*/ |
51
|
|
|
private $useOutputWalkers; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
private $count; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructor. |
60
|
|
|
* |
61
|
|
|
* @param Query|QueryBuilder $query A Doctrine ORM query or query builder. |
62
|
|
|
* @param boolean $fetchJoinCollection Whether the query joins a collection (true by default). |
63
|
|
|
*/ |
64
|
113 |
|
public function __construct($query, $fetchJoinCollection = true) |
65
|
|
|
{ |
66
|
113 |
|
if ($query instanceof QueryBuilder) { |
67
|
2 |
|
$query = $query->getQuery(); |
68
|
|
|
} |
69
|
|
|
|
70
|
113 |
|
$this->query = $query; |
71
|
113 |
|
$this->fetchJoinCollection = (bool) $fetchJoinCollection; |
72
|
113 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the query. |
76
|
|
|
* |
77
|
|
|
* @return Query |
78
|
|
|
*/ |
79
|
|
|
public function getQuery() |
80
|
|
|
{ |
81
|
|
|
return $this->query; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns whether the query joins a collection. |
86
|
|
|
* |
87
|
|
|
* @return boolean Whether the query joins a collection. |
88
|
|
|
*/ |
89
|
|
|
public function getFetchJoinCollection() |
90
|
|
|
{ |
91
|
|
|
return $this->fetchJoinCollection; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns whether the paginator will use an output walker. |
96
|
|
|
* |
97
|
|
|
* @return bool|null |
98
|
|
|
*/ |
99
|
|
|
public function getUseOutputWalkers() |
100
|
|
|
{ |
101
|
|
|
return $this->useOutputWalkers; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Sets whether the paginator will use an output walker. |
106
|
|
|
* |
107
|
|
|
* @param bool|null $useOutputWalkers |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
100 |
|
public function setUseOutputWalkers($useOutputWalkers) |
112
|
|
|
{ |
113
|
100 |
|
$this->useOutputWalkers = $useOutputWalkers; |
114
|
|
|
|
115
|
100 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
20 |
|
public function count() |
122
|
|
|
{ |
123
|
20 |
|
if ($this->count === null) { |
124
|
|
|
try { |
125
|
20 |
|
$this->count = array_sum(array_map('current', $this->getCountQuery()->getScalarResult())); |
126
|
2 |
|
} catch (NoResultException $e) { |
127
|
|
|
$this->count = 0; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
18 |
|
return $this->count; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
100 |
|
public function getIterator() |
138
|
|
|
{ |
139
|
100 |
|
$offset = $this->query->getFirstResult(); |
140
|
100 |
|
$length = $this->query->getMaxResults(); |
141
|
|
|
|
142
|
100 |
|
if ($this->fetchJoinCollection && $length !== null) { |
143
|
42 |
|
$subQuery = $this->cloneQuery($this->query); |
144
|
|
|
|
145
|
42 |
|
if ($this->useOutputWalker($subQuery)) { |
146
|
24 |
|
$subQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class); |
147
|
|
|
} else { |
148
|
18 |
|
$this->appendTreeWalker($subQuery, LimitSubqueryWalker::class); |
149
|
18 |
|
$this->appendTreeWalker($subQuery, RemoveUselessLeftJoinsWalker::class); |
150
|
18 |
|
$this->unbindUnusedQueryParams($subQuery); |
151
|
|
|
} |
152
|
|
|
|
153
|
39 |
|
$subQuery->setFirstResult($offset)->setMaxResults($length); |
154
|
|
|
|
155
|
39 |
|
$foundIdRows = $subQuery->getScalarResult(); |
156
|
|
|
|
157
|
|
|
// don't do this for an empty id array |
158
|
39 |
|
if ($foundIdRows === []) { |
159
|
2 |
|
return new \ArrayIterator([]); |
160
|
|
|
} |
161
|
|
|
|
162
|
38 |
|
$whereInQuery = $this->cloneQuery($this->query); |
163
|
38 |
|
$ids = array_map('current', $foundIdRows); |
164
|
|
|
|
165
|
38 |
|
$this->appendTreeWalker($whereInQuery, WhereInWalker::class); |
166
|
38 |
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids)); |
167
|
38 |
|
$whereInQuery->setFirstResult(null)->setMaxResults(null); |
168
|
38 |
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, $ids); |
169
|
38 |
|
$whereInQuery->setCacheable($this->query->isCacheable()); |
170
|
38 |
|
$whereInQuery->expireQueryCache(); |
171
|
|
|
|
172
|
38 |
|
$result = $whereInQuery->getResult($this->query->getHydrationMode()); |
173
|
|
|
} else { |
174
|
60 |
|
$result = $this->cloneQuery($this->query) |
175
|
60 |
|
->setMaxResults($length) |
176
|
60 |
|
->setFirstResult($offset) |
177
|
60 |
|
->setCacheable($this->query->isCacheable()) |
178
|
60 |
|
->getResult($this->query->getHydrationMode()) |
179
|
|
|
; |
180
|
|
|
} |
181
|
|
|
|
182
|
95 |
|
return new \ArrayIterator($result); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Clones a query. |
187
|
|
|
* |
188
|
|
|
* @param Query $query The query. |
189
|
|
|
* |
190
|
|
|
* @return Query The cloned query. |
191
|
|
|
*/ |
192
|
113 |
|
private function cloneQuery(Query $query) |
193
|
|
|
{ |
194
|
|
|
/* @var $cloneQuery Query */ |
195
|
113 |
|
$cloneQuery = clone $query; |
196
|
|
|
|
197
|
113 |
|
$cloneQuery->setParameters(clone $query->getParameters()); |
198
|
113 |
|
$cloneQuery->setCacheable(false); |
199
|
|
|
|
200
|
113 |
|
foreach ($query->getHints() as $name => $value) { |
201
|
3 |
|
$cloneQuery->setHint($name, $value); |
202
|
|
|
} |
203
|
|
|
|
204
|
113 |
|
return $cloneQuery; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Determines whether to use an output walker for the query. |
209
|
|
|
* |
210
|
|
|
* @param Query $query The query. |
211
|
|
|
* |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
58 |
|
private function useOutputWalker(Query $query) |
215
|
|
|
{ |
216
|
58 |
|
if ($this->useOutputWalkers === null) { |
217
|
8 |
|
return (bool) $query->getHint(Query::HINT_CUSTOM_OUTPUT_WALKER) === false; |
218
|
|
|
} |
219
|
|
|
|
220
|
50 |
|
return $this->useOutputWalkers; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Appends a custom tree walker to the tree walkers hint. |
225
|
|
|
* |
226
|
|
|
* @param Query $query |
227
|
|
|
* @param string $walkerClass |
228
|
|
|
*/ |
229
|
51 |
|
private function appendTreeWalker(Query $query, $walkerClass) |
230
|
|
|
{ |
231
|
51 |
|
$hints = $query->getHint(Query::HINT_CUSTOM_TREE_WALKERS); |
232
|
|
|
|
233
|
51 |
|
if ($hints === false) { |
234
|
50 |
|
$hints = []; |
235
|
|
|
} |
236
|
|
|
|
237
|
51 |
|
$hints[] = $walkerClass; |
238
|
51 |
|
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $hints); |
239
|
51 |
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns Query prepared to count. |
243
|
|
|
* |
244
|
|
|
* @return Query |
245
|
|
|
*/ |
246
|
20 |
|
private function getCountQuery() |
247
|
|
|
{ |
248
|
|
|
/* @var $countQuery Query */ |
249
|
20 |
|
$countQuery = $this->cloneQuery($this->query); |
250
|
|
|
|
251
|
20 |
|
if ( ! $countQuery->hasHint(CountWalker::HINT_DISTINCT)) { |
252
|
20 |
|
$countQuery->setHint(CountWalker::HINT_DISTINCT, true); |
253
|
|
|
} |
254
|
|
|
|
255
|
20 |
|
if ($this->useOutputWalker($countQuery)) { |
256
|
9 |
|
$platform = $countQuery->getEntityManager()->getConnection()->getDatabasePlatform(); // law of demeter win |
257
|
|
|
|
258
|
9 |
|
$rsm = new ResultSetMapping(); |
259
|
9 |
|
$rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count'); |
260
|
|
|
|
261
|
9 |
|
$countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, CountOutputWalker::class); |
262
|
9 |
|
$countQuery->setResultSetMapping($rsm); |
263
|
|
|
} else { |
264
|
12 |
|
$this->appendTreeWalker($countQuery, CountWalker::class); |
265
|
12 |
|
$this->unbindUnusedQueryParams($countQuery); |
266
|
|
|
} |
267
|
|
|
|
268
|
18 |
|
$countQuery->setFirstResult(null)->setMaxResults(null); |
269
|
|
|
|
270
|
18 |
|
return $countQuery; |
271
|
|
|
} |
272
|
|
|
|
273
|
27 |
|
private function unbindUnusedQueryParams(Query $query): void |
274
|
|
|
{ |
275
|
27 |
|
$parser = new Parser($query); |
276
|
27 |
|
$parameterMappings = $parser->parse()->getParameterMappings(); |
277
|
|
|
/* @var $parameters \Doctrine\Common\Collections\Collection|\Doctrine\ORM\Query\Parameter[] */ |
278
|
22 |
|
$parameters = $query->getParameters(); |
279
|
|
|
|
280
|
22 |
|
foreach ($parameters as $key => $parameter) { |
281
|
4 |
|
$parameterName = $parameter->getName(); |
282
|
|
|
|
283
|
4 |
|
if ( ! (isset($parameterMappings[$parameterName]) || array_key_exists($parameterName, $parameterMappings))) { |
284
|
4 |
|
unset($parameters[$key]); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
22 |
|
$query->setParameters($parameters); |
289
|
22 |
|
} |
290
|
|
|
} |
291
|
|
|
|