vendor/doctrine/orm/lib/Doctrine/ORM/Query.php line 325

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