vendor/doctrine/orm/src/Query.php line 291

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