|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\ORM; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\Cache; |
|
|
|
|
|
|
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
9
|
|
|
use Doctrine\DBAL\LockMode; |
|
10
|
|
|
use Doctrine\ORM\Internal\Hydration\IterableResult; |
|
11
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
12
|
|
|
use Doctrine\ORM\Query\AST\DeleteStatement; |
|
13
|
|
|
use Doctrine\ORM\Query\AST\SelectStatement; |
|
14
|
|
|
use Doctrine\ORM\Query\AST\UpdateStatement; |
|
15
|
|
|
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor; |
|
16
|
|
|
use Doctrine\ORM\Query\Parameter; |
|
17
|
|
|
use Doctrine\ORM\Query\ParameterTypeInferer; |
|
18
|
|
|
use Doctrine\ORM\Query\Parser; |
|
19
|
|
|
use Doctrine\ORM\Query\ParserResult; |
|
20
|
|
|
use Doctrine\ORM\Query\QueryException; |
|
21
|
|
|
use Doctrine\ORM\Utility\HierarchyDiscriminatorResolver; |
|
22
|
|
|
use function array_keys; |
|
23
|
|
|
use function array_values; |
|
24
|
|
|
use function assert; |
|
25
|
|
|
use function count; |
|
26
|
|
|
use function in_array; |
|
27
|
|
|
use function ksort; |
|
28
|
|
|
use function md5; |
|
29
|
|
|
use function reset; |
|
30
|
|
|
use function serialize; |
|
31
|
|
|
use function sha1; |
|
32
|
|
|
use function stripos; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* A Query object represents a DQL query. |
|
36
|
|
|
*/ |
|
37
|
|
|
final class Query extends AbstractQuery |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* A query object is in CLEAN state when it has NO unparsed/unprocessed DQL parts. |
|
41
|
|
|
*/ |
|
42
|
|
|
public const STATE_CLEAN = 1; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* A query object is in state DIRTY when it has DQL parts that have not yet been |
|
46
|
|
|
* parsed/processed. This is automatically defined as DIRTY when addDqlQueryPart |
|
47
|
|
|
* is called. |
|
48
|
|
|
*/ |
|
49
|
|
|
public const STATE_DIRTY = 2; |
|
50
|
|
|
|
|
51
|
|
|
/* Query HINTS */ |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The refresh hint turns any query into a refresh query with the result that |
|
55
|
|
|
* any local changes in entities are overridden with the fetched values. |
|
56
|
|
|
*/ |
|
57
|
|
|
public const HINT_REFRESH = 'doctrine.refresh'; |
|
58
|
|
|
|
|
59
|
|
|
public const HINT_CACHE_ENABLED = 'doctrine.cache.enabled'; |
|
60
|
|
|
|
|
61
|
|
|
public const HINT_CACHE_EVICT = 'doctrine.cache.evict'; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Internal hint: is set to the proxy entity that is currently triggered for loading |
|
65
|
|
|
*/ |
|
66
|
|
|
public const HINT_REFRESH_ENTITY = 'doctrine.refresh.entity'; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The forcePartialLoad query hint forces a particular query to return |
|
70
|
|
|
* partial objects. |
|
71
|
|
|
* |
|
72
|
|
|
* @todo Rename: HINT_OPTIMIZE |
|
73
|
|
|
*/ |
|
74
|
|
|
public const HINT_FORCE_PARTIAL_LOAD = 'doctrine.forcePartialLoad'; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* The includeMetaColumns query hint causes meta columns like foreign keys and |
|
78
|
|
|
* discriminator columns to be selected and returned as part of the query result. |
|
79
|
|
|
* |
|
80
|
|
|
* This hint does only apply to non-object queries. |
|
81
|
|
|
*/ |
|
82
|
|
|
public const HINT_INCLUDE_META_COLUMNS = 'doctrine.includeMetaColumns'; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* An array of class names that implement \Doctrine\ORM\Query\TreeWalker and |
|
86
|
|
|
* are iterated and executed after the DQL has been parsed into an AST. |
|
87
|
|
|
*/ |
|
88
|
|
|
public const HINT_CUSTOM_TREE_WALKERS = 'doctrine.customTreeWalkers'; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* A string with a class name that implements \Doctrine\ORM\Query\TreeWalker |
|
92
|
|
|
* and is used for generating the target SQL from any DQL AST tree. |
|
93
|
|
|
*/ |
|
94
|
|
|
public const HINT_CUSTOM_OUTPUT_WALKER = 'doctrine.customOutputWalker'; |
|
95
|
|
|
|
|
96
|
|
|
//const HINT_READ_ONLY = 'doctrine.readOnly'; |
|
97
|
|
|
|
|
98
|
|
|
public const HINT_INTERNAL_ITERATION = 'doctrine.internal.iteration'; |
|
99
|
|
|
|
|
100
|
|
|
public const HINT_LOCK_MODE = 'doctrine.lockMode'; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* The current state of this query. |
|
104
|
|
|
* |
|
105
|
|
|
* @var int |
|
106
|
|
|
*/ |
|
107
|
|
|
private $state = self::STATE_CLEAN; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* A snapshot of the parameter types the query was parsed with. |
|
111
|
|
|
* |
|
112
|
|
|
* @var mixed[] |
|
113
|
|
|
*/ |
|
114
|
|
|
private $parsedTypes = []; |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Cached DQL query. |
|
118
|
|
|
* |
|
119
|
|
|
* @var string |
|
120
|
|
|
*/ |
|
121
|
|
|
private $dql; |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* The parser result that holds DQL => SQL information. |
|
125
|
|
|
* |
|
126
|
|
|
* @var ParserResult |
|
127
|
|
|
*/ |
|
128
|
|
|
private $parserResult; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* The first result to return (the "offset"). |
|
132
|
|
|
* |
|
133
|
|
|
* @var int |
|
134
|
|
|
*/ |
|
135
|
|
|
private $firstResult; |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* The maximum number of results to return (the "limit"). |
|
139
|
|
|
* |
|
140
|
|
|
* @var int|null |
|
141
|
|
|
*/ |
|
142
|
|
|
private $maxResults; |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* The cache driver used for caching queries. |
|
146
|
|
|
* |
|
147
|
|
|
* @var Cache|null |
|
148
|
|
|
*/ |
|
149
|
|
|
private $queryCache; |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Whether or not expire the query cache. |
|
153
|
|
|
* |
|
154
|
|
|
* @var bool |
|
155
|
|
|
*/ |
|
156
|
|
|
private $expireQueryCache = false; |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* The query cache lifetime. |
|
160
|
|
|
* |
|
161
|
|
|
* @var int |
|
162
|
|
|
*/ |
|
163
|
|
|
private $queryCacheTTL; |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Whether to use a query cache, if available. Defaults to TRUE. |
|
167
|
|
|
* |
|
168
|
|
|
* @var bool |
|
169
|
|
|
*/ |
|
170
|
|
|
private $useQueryCache = true; |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Gets the SQL query/queries that correspond to this DQL query. |
|
174
|
|
|
* |
|
175
|
|
|
* @return mixed The built sql query or an array of all sql queries. |
|
176
|
|
|
* |
|
177
|
|
|
* @override |
|
178
|
|
|
*/ |
|
179
|
342 |
|
public function getSQL() |
|
180
|
|
|
{ |
|
181
|
342 |
|
return $this->parse()->getSqlExecutor()->getSqlStatements(); |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Returns the corresponding AST for this DQL query. |
|
186
|
|
|
* |
|
187
|
|
|
* @return SelectStatement|UpdateStatement|DeleteStatement |
|
188
|
|
|
*/ |
|
189
|
2 |
|
public function getAST() |
|
190
|
|
|
{ |
|
191
|
2 |
|
$parser = new Parser($this); |
|
192
|
|
|
|
|
193
|
2 |
|
return $parser->getAST(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* {@inheritdoc} |
|
198
|
|
|
*/ |
|
199
|
456 |
|
protected function getResultSetMapping() |
|
200
|
|
|
{ |
|
201
|
|
|
// parse query or load from cache |
|
202
|
456 |
|
if ($this->resultSetMapping === null) { |
|
203
|
42 |
|
$this->resultSetMapping = $this->parse()->getResultSetMapping(); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
450 |
|
return $this->resultSetMapping; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Parses the DQL query, if necessary, and stores the parser result. |
|
211
|
|
|
* |
|
212
|
|
|
* Note: Populates $this->parserResult as a side-effect. |
|
213
|
|
|
* |
|
214
|
|
|
* @return ParserResult |
|
215
|
|
|
*/ |
|
216
|
781 |
|
private function parse() |
|
217
|
|
|
{ |
|
218
|
781 |
|
$types = []; |
|
219
|
|
|
|
|
220
|
781 |
|
foreach ($this->parameters as $parameter) { |
|
221
|
|
|
/** @var Query\Parameter $parameter */ |
|
222
|
178 |
|
$types[$parameter->getName()] = $parameter->getType(); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// Return previous parser result if the query and the filter collection are both clean |
|
226
|
781 |
|
if ($this->state === self::STATE_CLEAN && $this->parsedTypes === $types && $this->em->isFiltersStateClean()) { |
|
227
|
40 |
|
return $this->parserResult; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
781 |
|
$this->state = self::STATE_CLEAN; |
|
231
|
781 |
|
$this->parsedTypes = $types; |
|
232
|
|
|
|
|
233
|
|
|
// Check query cache. |
|
234
|
781 |
|
$queryCache = $this->getQueryCacheDriver(); |
|
235
|
781 |
|
if (! ($this->useQueryCache && $queryCache)) { |
|
236
|
181 |
|
$parser = new Parser($this); |
|
237
|
|
|
|
|
238
|
181 |
|
$this->parserResult = $parser->parse(); |
|
239
|
|
|
|
|
240
|
177 |
|
return $this->parserResult; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
600 |
|
$hash = $this->getQueryCacheId(); |
|
244
|
600 |
|
$cached = $this->expireQueryCache ? false : $queryCache->fetch($hash); |
|
245
|
|
|
|
|
246
|
600 |
|
if ($cached instanceof ParserResult) { |
|
247
|
|
|
// Cache hit. |
|
248
|
139 |
|
$this->parserResult = $cached; |
|
249
|
|
|
|
|
250
|
139 |
|
return $this->parserResult; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
// Cache miss. |
|
254
|
548 |
|
$parser = new Parser($this); |
|
255
|
|
|
|
|
256
|
548 |
|
$this->parserResult = $parser->parse(); |
|
257
|
|
|
|
|
258
|
529 |
|
$queryCache->save($hash, $this->parserResult, $this->queryCacheTTL); |
|
259
|
|
|
|
|
260
|
529 |
|
return $this->parserResult; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* {@inheritdoc} |
|
265
|
|
|
*/ |
|
266
|
468 |
|
protected function doExecute() |
|
267
|
|
|
{ |
|
268
|
468 |
|
$executor = $this->parse()->getSqlExecutor(); |
|
269
|
|
|
|
|
270
|
463 |
|
if ($this->queryCacheProfile) { |
|
271
|
8 |
|
$executor->setQueryCacheProfile($this->queryCacheProfile); |
|
272
|
|
|
} else { |
|
273
|
457 |
|
$executor->removeQueryCacheProfile(); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
463 |
|
if ($this->resultSetMapping === null) { |
|
277
|
420 |
|
$this->resultSetMapping = $this->parserResult->getResultSetMapping(); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
// Prepare parameters |
|
281
|
463 |
|
$paramMappings = $this->parserResult->getParameterMappings(); |
|
282
|
463 |
|
$paramCount = count($this->parameters); |
|
283
|
463 |
|
$mappingCount = count($paramMappings); |
|
284
|
|
|
|
|
285
|
463 |
|
if ($paramCount > $mappingCount) { |
|
286
|
2 |
|
throw QueryException::tooManyParameters($mappingCount, $paramCount); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
462 |
|
if ($paramCount < $mappingCount) { |
|
290
|
1 |
|
throw QueryException::tooFewParameters($mappingCount, $paramCount); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
// evict all cache for the entity region |
|
294
|
461 |
|
if ($this->hasCache && isset($this->hints[self::HINT_CACHE_EVICT]) && $this->hints[self::HINT_CACHE_EVICT]) { |
|
295
|
2 |
|
$this->evictEntityCacheRegion(); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
461 |
|
[$sqlParams, $types] = $this->processParameterMappings($paramMappings); |
|
299
|
|
|
|
|
300
|
460 |
|
$this->evictResultSetCache( |
|
301
|
460 |
|
$executor, |
|
302
|
460 |
|
$sqlParams, |
|
303
|
460 |
|
$types, |
|
304
|
460 |
|
$this->em->getConnection()->getParams() |
|
305
|
|
|
); |
|
306
|
|
|
|
|
307
|
460 |
|
return $executor->execute($this->em->getConnection(), $sqlParams, $types); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @param mixed[] $sqlParams |
|
312
|
|
|
* @param mixed[] $types |
|
313
|
|
|
* @param mixed[] $connectionParams |
|
314
|
|
|
*/ |
|
315
|
460 |
|
private function evictResultSetCache( |
|
316
|
|
|
AbstractSqlExecutor $executor, |
|
317
|
|
|
array $sqlParams, |
|
318
|
|
|
array $types, |
|
319
|
|
|
array $connectionParams |
|
320
|
|
|
) { |
|
321
|
460 |
|
if ($this->queryCacheProfile === null || ! $this->getExpireResultCache()) { |
|
322
|
460 |
|
return; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
2 |
|
$cacheDriver = $this->queryCacheProfile->getResultCacheDriver(); |
|
326
|
2 |
|
$statements = (array) $executor->getSqlStatements(); // Type casted since it can either be a string or an array |
|
327
|
|
|
|
|
328
|
2 |
|
foreach ($statements as $statement) { |
|
329
|
2 |
|
$cacheKeys = $this->queryCacheProfile->generateCacheKeys($statement, $sqlParams, $types, $connectionParams); |
|
330
|
|
|
|
|
331
|
2 |
|
$cacheDriver->delete(reset($cacheKeys)); |
|
332
|
|
|
} |
|
333
|
2 |
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Evict entity cache region |
|
337
|
|
|
*/ |
|
338
|
2 |
|
private function evictEntityCacheRegion() |
|
339
|
|
|
{ |
|
340
|
2 |
|
$AST = $this->getAST(); |
|
341
|
|
|
|
|
342
|
2 |
|
if ($AST instanceof SelectStatement) { |
|
343
|
|
|
throw new QueryException('The hint "HINT_CACHE_EVICT" is not valid for select statements.'); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
2 |
|
$className = $AST instanceof DeleteStatement |
|
347
|
1 |
|
? $AST->deleteClause->abstractSchemaName |
|
348
|
2 |
|
: $AST->updateClause->abstractSchemaName; |
|
349
|
|
|
|
|
350
|
2 |
|
$this->em->getCache()->evictEntityRegion($className); |
|
351
|
2 |
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Processes query parameter mappings. |
|
355
|
|
|
* |
|
356
|
|
|
* @param mixed[] $paramMappings |
|
357
|
|
|
* |
|
358
|
|
|
* @return mixed[][] |
|
359
|
|
|
* |
|
360
|
|
|
* @throws Query\QueryException |
|
361
|
|
|
*/ |
|
362
|
461 |
|
private function processParameterMappings($paramMappings) |
|
363
|
|
|
{ |
|
364
|
461 |
|
$sqlParams = []; |
|
365
|
461 |
|
$types = []; |
|
366
|
|
|
|
|
367
|
461 |
|
foreach ($this->parameters as $parameter) { |
|
368
|
165 |
|
$key = $parameter->getName(); |
|
369
|
|
|
|
|
370
|
165 |
|
if (! isset($paramMappings[$key])) { |
|
371
|
1 |
|
throw QueryException::unknownParameter($key); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
164 |
|
[$value, $type] = $this->resolveParameterValue($parameter); |
|
375
|
|
|
|
|
376
|
164 |
|
foreach ($paramMappings[$key] as $position) { |
|
377
|
164 |
|
$types[$position] = $type; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
164 |
|
$sqlPositions = $paramMappings[$key]; |
|
381
|
164 |
|
$sqlPositionsCount = count($sqlPositions); |
|
382
|
|
|
|
|
383
|
|
|
// optimized multi value sql positions away for now, |
|
384
|
|
|
// they are not allowed in DQL anyways. |
|
385
|
164 |
|
$value = [$value]; |
|
386
|
164 |
|
$countValue = count($value); |
|
387
|
|
|
|
|
388
|
164 |
|
for ($i = 0, $l = $sqlPositionsCount; $i < $l; $i++) { |
|
389
|
164 |
|
$sqlParams[$sqlPositions[$i]] = $value[($i % $countValue)]; |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
460 |
|
if (count($sqlParams) !== count($types)) { |
|
394
|
|
|
throw QueryException::parameterTypeMismatch(); |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
460 |
|
if ($sqlParams) { |
|
398
|
164 |
|
ksort($sqlParams); |
|
399
|
164 |
|
$sqlParams = array_values($sqlParams); |
|
400
|
|
|
|
|
401
|
164 |
|
ksort($types); |
|
402
|
164 |
|
$types = array_values($types); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
460 |
|
return [$sqlParams, $types]; |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
/** @return mixed[] tuple of (value, type) */ |
|
409
|
164 |
|
private function resolveParameterValue(Parameter $parameter) : array |
|
410
|
|
|
{ |
|
411
|
164 |
|
if ($parameter->typeWasSpecified()) { |
|
412
|
5 |
|
return [$parameter->getValue(), $parameter->getType()]; |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
160 |
|
$key = $parameter->getName(); |
|
416
|
160 |
|
$originalValue = $parameter->getValue(); |
|
417
|
160 |
|
$value = $originalValue; |
|
418
|
160 |
|
$rsm = $this->getResultSetMapping(); |
|
419
|
|
|
|
|
420
|
160 |
|
assert($rsm !== null); |
|
421
|
|
|
|
|
422
|
160 |
|
if ($value instanceof ClassMetadata && isset($rsm->metadataParameterMapping[$key])) { |
|
423
|
|
|
$value = $value->getMetadataValue($rsm->metadataParameterMapping[$key]); |
|
|
|
|
|
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
160 |
|
if ($value instanceof ClassMetadata && isset($rsm->discriminatorParameters[$key])) { |
|
427
|
3 |
|
$value = array_keys(HierarchyDiscriminatorResolver::resolveDiscriminatorsForClass($value, $this->em)); |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
160 |
|
$processedValue = $this->processParameterValue($value); |
|
431
|
|
|
|
|
432
|
|
|
return [ |
|
433
|
160 |
|
$processedValue, |
|
434
|
160 |
|
$originalValue === $processedValue |
|
435
|
149 |
|
? $parameter->getType() |
|
436
|
160 |
|
: ParameterTypeInferer::inferType($processedValue), |
|
437
|
|
|
]; |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
/** |
|
441
|
|
|
* Defines a cache driver to be used for caching queries. |
|
442
|
|
|
* |
|
443
|
|
|
* @param Cache|null $queryCache Cache driver. |
|
444
|
|
|
* |
|
445
|
|
|
* @return Query This query instance. |
|
446
|
|
|
*/ |
|
447
|
6 |
|
public function setQueryCacheDriver($queryCache) |
|
448
|
|
|
{ |
|
449
|
6 |
|
$this->queryCache = $queryCache; |
|
450
|
|
|
|
|
451
|
6 |
|
return $this; |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* Defines whether the query should make use of a query cache, if available. |
|
456
|
|
|
* |
|
457
|
|
|
* @param bool $bool |
|
458
|
|
|
* |
|
459
|
|
|
* @return Query This query instance. |
|
460
|
|
|
*/ |
|
461
|
184 |
|
public function useQueryCache($bool) |
|
462
|
|
|
{ |
|
463
|
184 |
|
$this->useQueryCache = $bool; |
|
464
|
|
|
|
|
465
|
184 |
|
return $this; |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
/** |
|
469
|
|
|
* Returns the cache driver used for query caching. |
|
470
|
|
|
* |
|
471
|
|
|
* @return Cache|null The cache driver used for query caching or NULL, if |
|
472
|
|
|
* this Query does not use query caching. |
|
473
|
|
|
*/ |
|
474
|
781 |
|
public function getQueryCacheDriver() |
|
475
|
|
|
{ |
|
476
|
781 |
|
if ($this->queryCache) { |
|
477
|
9 |
|
return $this->queryCache; |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
772 |
|
return $this->em->getConfiguration()->getQueryCacheImpl(); |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
|
|
/** |
|
484
|
|
|
* Defines how long the query cache will be active before expire. |
|
485
|
|
|
* |
|
486
|
|
|
* @param int $timeToLive How long the cache entry is valid. |
|
487
|
|
|
* |
|
488
|
|
|
* @return Query This query instance. |
|
489
|
|
|
*/ |
|
490
|
1 |
|
public function setQueryCacheLifetime($timeToLive) |
|
491
|
|
|
{ |
|
492
|
1 |
|
if ($timeToLive !== null) { |
|
|
|
|
|
|
493
|
1 |
|
$timeToLive = (int) $timeToLive; |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
1 |
|
$this->queryCacheTTL = $timeToLive; |
|
497
|
|
|
|
|
498
|
1 |
|
return $this; |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
/** |
|
502
|
|
|
* Retrieves the lifetime of resultset cache. |
|
503
|
|
|
* |
|
504
|
|
|
* @return int |
|
505
|
|
|
*/ |
|
506
|
|
|
public function getQueryCacheLifetime() |
|
507
|
|
|
{ |
|
508
|
|
|
return $this->queryCacheTTL; |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
/** |
|
512
|
|
|
* Defines if the query cache is active or not. |
|
513
|
|
|
* |
|
514
|
|
|
* @param bool $expire Whether or not to force query cache expiration. |
|
515
|
|
|
* |
|
516
|
|
|
* @return Query This query instance. |
|
517
|
|
|
*/ |
|
518
|
7 |
|
public function expireQueryCache($expire = true) |
|
519
|
|
|
{ |
|
520
|
7 |
|
$this->expireQueryCache = $expire; |
|
521
|
|
|
|
|
522
|
7 |
|
return $this; |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
/** |
|
526
|
|
|
* Retrieves if the query cache is active or not. |
|
527
|
|
|
* |
|
528
|
|
|
* @return bool |
|
529
|
|
|
*/ |
|
530
|
|
|
public function getExpireQueryCache() |
|
531
|
|
|
{ |
|
532
|
|
|
return $this->expireQueryCache; |
|
533
|
|
|
} |
|
534
|
|
|
|
|
535
|
220 |
|
public function free() |
|
536
|
|
|
{ |
|
537
|
220 |
|
parent::free(); |
|
538
|
|
|
|
|
539
|
220 |
|
$this->dql = null; |
|
540
|
220 |
|
$this->state = self::STATE_CLEAN; |
|
541
|
220 |
|
} |
|
542
|
|
|
|
|
543
|
|
|
/** |
|
544
|
|
|
* Sets a DQL query string. |
|
545
|
|
|
* |
|
546
|
|
|
* @param string $dqlQuery DQL Query. |
|
547
|
|
|
* |
|
548
|
|
|
* @return AbstractQuery |
|
549
|
|
|
*/ |
|
550
|
966 |
|
public function setDQL($dqlQuery) |
|
551
|
|
|
{ |
|
552
|
966 |
|
if ($dqlQuery !== null) { |
|
|
|
|
|
|
553
|
966 |
|
$this->dql = $dqlQuery; |
|
554
|
966 |
|
$this->state = self::STATE_DIRTY; |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
966 |
|
return $this; |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
/** |
|
561
|
|
|
* Returns the DQL query that is represented by this query object. |
|
562
|
|
|
* |
|
563
|
|
|
* @return string DQL query. |
|
564
|
|
|
*/ |
|
565
|
911 |
|
public function getDQL() |
|
566
|
|
|
{ |
|
567
|
911 |
|
return $this->dql; |
|
568
|
|
|
} |
|
569
|
|
|
|
|
570
|
|
|
/** |
|
571
|
|
|
* Returns the state of this query object |
|
572
|
|
|
* By default the type is Doctrine_ORM_Query_Abstract::STATE_CLEAN but if it appears any unprocessed DQL |
|
573
|
|
|
* part, it is switched to Doctrine_ORM_Query_Abstract::STATE_DIRTY. |
|
574
|
|
|
* |
|
575
|
|
|
* @see AbstractQuery::STATE_CLEAN |
|
576
|
|
|
* @see AbstractQuery::STATE_DIRTY |
|
577
|
|
|
* |
|
578
|
|
|
* @return int The query state. |
|
579
|
|
|
*/ |
|
580
|
|
|
public function getState() |
|
581
|
|
|
{ |
|
582
|
|
|
return $this->state; |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
/** |
|
586
|
|
|
* Method to check if an arbitrary piece of DQL exists |
|
587
|
|
|
* |
|
588
|
|
|
* @param string $dql Arbitrary piece of DQL to check for. |
|
589
|
|
|
* |
|
590
|
|
|
* @return bool |
|
591
|
|
|
*/ |
|
592
|
|
|
public function contains($dql) |
|
593
|
|
|
{ |
|
594
|
|
|
return stripos($this->getDQL(), $dql) !== false; |
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
|
|
/** |
|
598
|
|
|
* Sets the position of the first result to retrieve (the "offset"). |
|
599
|
|
|
* |
|
600
|
|
|
* @param int $firstResult The first result to return. |
|
601
|
|
|
* |
|
602
|
|
|
* @return Query This query object. |
|
603
|
|
|
*/ |
|
604
|
221 |
|
public function setFirstResult($firstResult) |
|
605
|
|
|
{ |
|
606
|
221 |
|
$this->firstResult = $firstResult; |
|
607
|
221 |
|
$this->state = self::STATE_DIRTY; |
|
608
|
|
|
|
|
609
|
221 |
|
return $this; |
|
610
|
|
|
} |
|
611
|
|
|
|
|
612
|
|
|
/** |
|
613
|
|
|
* Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
614
|
|
|
* Returns NULL if {@link setFirstResult} was not applied to this query. |
|
615
|
|
|
* |
|
616
|
|
|
* @return int The position of the first result. |
|
617
|
|
|
*/ |
|
618
|
669 |
|
public function getFirstResult() |
|
619
|
|
|
{ |
|
620
|
669 |
|
return $this->firstResult; |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* Sets the maximum number of results to retrieve (the "limit"). |
|
625
|
|
|
* |
|
626
|
|
|
* @param int|null $maxResults |
|
627
|
|
|
* |
|
628
|
|
|
* @return Query This query object. |
|
629
|
|
|
*/ |
|
630
|
245 |
|
public function setMaxResults($maxResults) |
|
631
|
|
|
{ |
|
632
|
245 |
|
$this->maxResults = $maxResults; |
|
633
|
245 |
|
$this->state = self::STATE_DIRTY; |
|
634
|
|
|
|
|
635
|
245 |
|
return $this; |
|
636
|
|
|
} |
|
637
|
|
|
|
|
638
|
|
|
/** |
|
639
|
|
|
* Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
640
|
|
|
* Returns NULL if {@link setMaxResults} was not applied to this query. |
|
641
|
|
|
* |
|
642
|
|
|
* @return int|null Maximum number of results. |
|
643
|
|
|
*/ |
|
644
|
669 |
|
public function getMaxResults() |
|
645
|
|
|
{ |
|
646
|
669 |
|
return $this->maxResults; |
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
/** |
|
650
|
|
|
* Executes the query and returns an IterableResult that can be used to incrementally |
|
651
|
|
|
* iterated over the result. |
|
652
|
|
|
* |
|
653
|
|
|
* @param ArrayCollection|array|Parameter[]|mixed[]|null $parameters The query parameters. |
|
654
|
|
|
* @param int $hydrationMode The hydration mode to use. |
|
655
|
|
|
* |
|
656
|
|
|
* @return IterableResult |
|
657
|
|
|
*/ |
|
658
|
10 |
|
public function iterate($parameters = null, $hydrationMode = self::HYDRATE_OBJECT) |
|
659
|
|
|
{ |
|
660
|
10 |
|
$this->setHint(self::HINT_INTERNAL_ITERATION, true); |
|
661
|
|
|
|
|
662
|
10 |
|
return parent::iterate($parameters, $hydrationMode); |
|
663
|
|
|
} |
|
664
|
|
|
|
|
665
|
|
|
/** {@inheritDoc} */ |
|
666
|
26 |
|
public function getIterable($parameters = null, $hydrationMode = self::HYDRATE_OBJECT) : iterable |
|
667
|
|
|
{ |
|
668
|
26 |
|
$this->setHint(self::HINT_INTERNAL_ITERATION, true); |
|
669
|
|
|
|
|
670
|
26 |
|
return parent::getIterable($parameters, $hydrationMode); |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
/** |
|
674
|
|
|
* {@inheritdoc} |
|
675
|
|
|
*/ |
|
676
|
487 |
|
public function setHint($name, $value) |
|
677
|
|
|
{ |
|
678
|
487 |
|
$this->state = self::STATE_DIRTY; |
|
679
|
|
|
|
|
680
|
487 |
|
return parent::setHint($name, $value); |
|
681
|
|
|
} |
|
682
|
|
|
|
|
683
|
|
|
/** |
|
684
|
|
|
* {@inheritdoc} |
|
685
|
|
|
*/ |
|
686
|
369 |
|
public function setHydrationMode($hydrationMode) |
|
687
|
|
|
{ |
|
688
|
369 |
|
$this->state = self::STATE_DIRTY; |
|
689
|
|
|
|
|
690
|
369 |
|
return parent::setHydrationMode($hydrationMode); |
|
691
|
|
|
} |
|
692
|
|
|
|
|
693
|
|
|
/** |
|
694
|
|
|
* Set the lock mode for this Query. |
|
695
|
|
|
* |
|
696
|
|
|
* @see \Doctrine\DBAL\LockMode |
|
697
|
|
|
* |
|
698
|
|
|
* @param int $lockMode |
|
699
|
|
|
* |
|
700
|
|
|
* @return Query |
|
701
|
|
|
* |
|
702
|
|
|
* @throws TransactionRequiredException |
|
703
|
|
|
*/ |
|
704
|
|
|
public function setLockMode($lockMode) |
|
705
|
|
|
{ |
|
706
|
|
|
if (in_array($lockMode, [LockMode::NONE, LockMode::PESSIMISTIC_READ, LockMode::PESSIMISTIC_WRITE], true)) { |
|
707
|
|
|
if (! $this->em->getConnection()->isTransactionActive()) { |
|
708
|
|
|
throw TransactionRequiredException::transactionRequired(); |
|
709
|
|
|
} |
|
710
|
|
|
} |
|
711
|
|
|
|
|
712
|
|
|
$this->setHint(self::HINT_LOCK_MODE, $lockMode); |
|
713
|
|
|
|
|
714
|
|
|
return $this; |
|
715
|
|
|
} |
|
716
|
|
|
|
|
717
|
|
|
/** |
|
718
|
|
|
* Get the current lock mode for this query. |
|
719
|
|
|
* |
|
720
|
|
|
* @return int|null The current lock mode of this query or NULL if no specific lock mode is set. |
|
721
|
|
|
*/ |
|
722
|
|
|
public function getLockMode() |
|
723
|
|
|
{ |
|
724
|
|
|
$lockMode = $this->getHint(self::HINT_LOCK_MODE); |
|
725
|
|
|
|
|
726
|
|
|
if ($lockMode === false) { |
|
727
|
|
|
return null; |
|
728
|
|
|
} |
|
729
|
|
|
|
|
730
|
|
|
return $lockMode; |
|
731
|
|
|
} |
|
732
|
|
|
|
|
733
|
|
|
/** |
|
734
|
|
|
* Generate a cache id for the query cache - reusing the Result-Cache-Id generator. |
|
735
|
|
|
* |
|
736
|
|
|
* @return string |
|
737
|
|
|
*/ |
|
738
|
600 |
|
protected function getQueryCacheId() |
|
739
|
|
|
{ |
|
740
|
600 |
|
ksort($this->hints); |
|
741
|
|
|
|
|
742
|
600 |
|
$platform = $this->getEntityManager() |
|
743
|
600 |
|
->getConnection() |
|
744
|
600 |
|
->getDatabasePlatform() |
|
745
|
600 |
|
->getName(); |
|
746
|
|
|
|
|
747
|
600 |
|
return md5( |
|
748
|
600 |
|
$this->getDQL() . serialize($this->hints) . |
|
749
|
600 |
|
'&platform=' . $platform . |
|
750
|
600 |
|
($this->em->hasFilters() ? $this->em->getFilters()->getHash() : '') . |
|
751
|
600 |
|
'&firstResult=' . $this->firstResult . '&maxResult=' . $this->maxResults . |
|
752
|
600 |
|
'&hydrationMode=' . $this->hydrationMode . '&types=' . serialize($this->parsedTypes) . 'DOCTRINE_QUERY_CACHE_SALT' |
|
753
|
|
|
); |
|
754
|
|
|
} |
|
755
|
|
|
|
|
756
|
|
|
/** |
|
757
|
|
|
* {@inheritdoc} |
|
758
|
|
|
*/ |
|
759
|
28 |
|
protected function getHash() |
|
760
|
|
|
{ |
|
761
|
28 |
|
return sha1(parent::getHash() . '-' . $this->firstResult . '-' . $this->maxResults); |
|
762
|
|
|
} |
|
763
|
|
|
|
|
764
|
|
|
/** |
|
765
|
|
|
* Cleanup Query resource when clone is called. |
|
766
|
|
|
*/ |
|
767
|
142 |
|
public function __clone() |
|
768
|
|
|
{ |
|
769
|
142 |
|
parent::__clone(); |
|
770
|
|
|
|
|
771
|
142 |
|
$this->state = self::STATE_DIRTY; |
|
772
|
142 |
|
} |
|
773
|
|
|
} |
|
774
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: