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
|
110 |
|
public function __construct($query, $fetchJoinCollection = true) |
65
|
|
|
{ |
66
|
110 |
|
if ($query instanceof QueryBuilder) { |
67
|
2 |
|
$query = $query->getQuery(); |
68
|
|
|
} |
69
|
|
|
|
70
|
110 |
|
$this->query = $query; |
71
|
110 |
|
$this->fetchJoinCollection = (bool) $fetchJoinCollection; |
72
|
110 |
|
} |
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
|
97 |
|
public function setUseOutputWalkers($useOutputWalkers) |
112
|
|
|
{ |
113
|
97 |
|
$this->useOutputWalkers = $useOutputWalkers; |
114
|
|
|
|
115
|
97 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
18 |
|
public function count() |
122
|
|
|
{ |
123
|
18 |
|
if ($this->count === null) { |
124
|
|
|
try { |
125
|
18 |
|
$this->count = array_sum(array_map('current', $this->getCountQuery()->getScalarResult())); |
126
|
2 |
|
} catch (NoResultException $e) { |
127
|
|
|
$this->count = 0; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
16 |
|
return $this->count; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
97 |
|
public function getIterator() |
138
|
|
|
{ |
139
|
97 |
|
$offset = $this->query->getFirstResult(); |
140
|
97 |
|
$length = $this->query->getMaxResults(); |
141
|
|
|
|
142
|
97 |
|
if ($this->fetchJoinCollection) { |
143
|
70 |
|
$subQuery = $this->cloneQuery($this->query); |
144
|
|
|
|
145
|
70 |
|
if ($this->useOutputWalker($subQuery)) { |
146
|
43 |
|
$subQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class); |
147
|
|
|
} else { |
148
|
27 |
|
$this->appendTreeWalker($subQuery, LimitSubqueryWalker::class); |
149
|
27 |
|
$this->unbindUnusedQueryParams($subQuery); |
150
|
|
|
} |
151
|
|
|
|
152
|
67 |
|
$subQuery->setFirstResult($offset)->setMaxResults($length); |
153
|
|
|
|
154
|
67 |
|
$foundIdRows = $subQuery->getScalarResult(); |
155
|
|
|
|
156
|
|
|
// don't do this for an empty id array |
157
|
67 |
|
if ($foundIdRows === []) { |
158
|
2 |
|
return new \ArrayIterator([]); |
159
|
|
|
} |
160
|
|
|
|
161
|
66 |
|
$whereInQuery = $this->cloneQuery($this->query); |
162
|
66 |
|
$ids = array_map('current', $foundIdRows); |
163
|
|
|
|
164
|
66 |
|
$this->appendTreeWalker($whereInQuery, WhereInWalker::class); |
165
|
66 |
|
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids)); |
166
|
66 |
|
$whereInQuery->setFirstResult(null)->setMaxResults(null); |
167
|
66 |
|
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, $ids); |
168
|
66 |
|
$whereInQuery->setCacheable($this->query->isCacheable()); |
169
|
66 |
|
$whereInQuery->expireQueryCache(); |
170
|
|
|
|
171
|
66 |
|
$result = $whereInQuery->getResult($this->query->getHydrationMode()); |
172
|
|
|
} else { |
173
|
27 |
|
$result = $this->cloneQuery($this->query) |
174
|
27 |
|
->setMaxResults($length) |
175
|
27 |
|
->setFirstResult($offset) |
176
|
27 |
|
->setCacheable($this->query->isCacheable()) |
177
|
27 |
|
->getResult($this->query->getHydrationMode()) |
178
|
|
|
; |
179
|
|
|
} |
180
|
|
|
|
181
|
92 |
|
return new \ArrayIterator($result); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Clones a query. |
186
|
|
|
* |
187
|
|
|
* @param Query $query The query. |
188
|
|
|
* |
189
|
|
|
* @return Query The cloned query. |
190
|
|
|
*/ |
191
|
110 |
|
private function cloneQuery(Query $query) |
192
|
|
|
{ |
193
|
|
|
/* @var $cloneQuery Query */ |
194
|
110 |
|
$cloneQuery = clone $query; |
195
|
|
|
|
196
|
110 |
|
$cloneQuery->setParameters(clone $query->getParameters()); |
197
|
110 |
|
$cloneQuery->setCacheable(false); |
198
|
|
|
|
199
|
110 |
|
foreach ($query->getHints() as $name => $value) { |
200
|
3 |
|
$cloneQuery->setHint($name, $value); |
201
|
|
|
} |
202
|
|
|
|
203
|
110 |
|
return $cloneQuery; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Determines whether to use an output walker for the query. |
208
|
|
|
* |
209
|
|
|
* @param Query $query The query. |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
84 |
|
private function useOutputWalker(Query $query) |
214
|
|
|
{ |
215
|
84 |
|
if ($this->useOutputWalkers === null) { |
216
|
13 |
|
return (bool) $query->getHint(Query::HINT_CUSTOM_OUTPUT_WALKER) === false; |
217
|
|
|
} |
218
|
|
|
|
219
|
71 |
|
return $this->useOutputWalkers; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Appends a custom tree walker to the tree walkers hint. |
224
|
|
|
* |
225
|
|
|
* @param Query $query |
226
|
|
|
* @param string $walkerClass |
227
|
|
|
*/ |
228
|
77 |
|
private function appendTreeWalker(Query $query, $walkerClass) |
229
|
|
|
{ |
230
|
77 |
|
$hints = $query->getHint(Query::HINT_CUSTOM_TREE_WALKERS); |
231
|
|
|
|
232
|
77 |
|
if ($hints === false) { |
233
|
76 |
|
$hints = []; |
234
|
|
|
} |
235
|
|
|
|
236
|
77 |
|
$hints[] = $walkerClass; |
237
|
77 |
|
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $hints); |
238
|
77 |
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Returns Query prepared to count. |
242
|
|
|
* |
243
|
|
|
* @return Query |
244
|
|
|
*/ |
245
|
18 |
|
private function getCountQuery() |
246
|
|
|
{ |
247
|
|
|
/* @var $countQuery Query */ |
248
|
18 |
|
$countQuery = $this->cloneQuery($this->query); |
249
|
|
|
|
250
|
18 |
|
if ( ! $countQuery->hasHint(CountWalker::HINT_DISTINCT)) { |
251
|
18 |
|
$countQuery->setHint(CountWalker::HINT_DISTINCT, true); |
252
|
|
|
} |
253
|
|
|
|
254
|
18 |
|
if ($this->useOutputWalker($countQuery)) { |
255
|
9 |
|
$platform = $countQuery->getEntityManager()->getConnection()->getDatabasePlatform(); // law of demeter win |
256
|
|
|
|
257
|
9 |
|
$rsm = new ResultSetMapping(); |
258
|
9 |
|
$rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count'); |
259
|
|
|
|
260
|
9 |
|
$countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, CountOutputWalker::class); |
261
|
9 |
|
$countQuery->setResultSetMapping($rsm); |
262
|
|
|
} else { |
263
|
10 |
|
$this->appendTreeWalker($countQuery, CountWalker::class); |
264
|
10 |
|
$this->unbindUnusedQueryParams($countQuery); |
265
|
|
|
} |
266
|
|
|
|
267
|
16 |
|
$countQuery->setFirstResult(null)->setMaxResults(null); |
268
|
|
|
|
269
|
16 |
|
return $countQuery; |
270
|
|
|
} |
271
|
|
|
|
272
|
34 |
|
private function unbindUnusedQueryParams(Query $query): void |
273
|
|
|
{ |
274
|
34 |
|
$parser = new Parser($query); |
275
|
34 |
|
$parameterMappings = $parser->parse()->getParameterMappings(); |
276
|
|
|
/* @var $parameters \Doctrine\Common\Collections\Collection|\Doctrine\ORM\Query\Parameter[] */ |
277
|
29 |
|
$parameters = $query->getParameters(); |
278
|
|
|
|
279
|
29 |
|
foreach ($parameters as $key => $parameter) { |
280
|
4 |
|
$parameterName = $parameter->getName(); |
281
|
|
|
|
282
|
4 |
|
if ( ! (isset($parameterMappings[$parameterName]) || array_key_exists($parameterName, $parameterMappings))) { |
283
|
4 |
|
unset($parameters[$key]); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
29 |
|
$query->setParameters($parameters); |
288
|
29 |
|
} |
289
|
|
|
} |
290
|
|
|
|