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 is_int;
  33. use function ksort;
  34. use function md5;
  35. use function method_exists;
  36. use function reset;
  37. use function serialize;
  38. use function sha1;
  39. use function stripos;
  40. /**
  41.  * A Query object represents a DQL query.
  42.  */
  43. final class Query extends AbstractQuery
  44. {
  45.     /**
  46.      * A query object is in CLEAN state when it has NO unparsed/unprocessed DQL parts.
  47.      */
  48.     public const STATE_CLEAN 1;
  49.     /**
  50.      * A query object is in state DIRTY when it has DQL parts that have not yet been
  51.      * parsed/processed. This is automatically defined as DIRTY when addDqlQueryPart
  52.      * is called.
  53.      */
  54.     public const STATE_DIRTY 2;
  55.     /* Query HINTS */
  56.     /**
  57.      * The refresh hint turns any query into a refresh query with the result that
  58.      * any local changes in entities are overridden with the fetched values.
  59.      */
  60.     public const HINT_REFRESH 'doctrine.refresh';
  61.     public const HINT_CACHE_ENABLED 'doctrine.cache.enabled';
  62.     public const HINT_CACHE_EVICT 'doctrine.cache.evict';
  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.      * The forcePartialLoad query hint forces a particular query to return
  69.      * partial objects.
  70.      *
  71.      * @todo Rename: HINT_OPTIMIZE
  72.      */
  73.     public const HINT_FORCE_PARTIAL_LOAD 'doctrine.forcePartialLoad';
  74.     /**
  75.      * The includeMetaColumns query hint causes meta columns like foreign keys and
  76.      * discriminator columns to be selected and returned as part of the query result.
  77.      *
  78.      * This hint does only apply to non-object queries.
  79.      */
  80.     public const HINT_INCLUDE_META_COLUMNS 'doctrine.includeMetaColumns';
  81.     /**
  82.      * An array of class names that implement \Doctrine\ORM\Query\TreeWalker and
  83.      * are iterated and executed after the DQL has been parsed into an AST.
  84.      */
  85.     public const HINT_CUSTOM_TREE_WALKERS 'doctrine.customTreeWalkers';
  86.     /**
  87.      * A string with a class name that implements \Doctrine\ORM\Query\TreeWalker
  88.      * and is used for generating the target SQL from any DQL AST tree.
  89.      */
  90.     public const HINT_CUSTOM_OUTPUT_WALKER 'doctrine.customOutputWalker';
  91.     /**
  92.      * Marks queries as creating only read only objects.
  93.      *
  94.      * If the object retrieved from the query is already in the identity map
  95.      * then it does not get marked as read only if it wasn't already.
  96.      */
  97.     public const HINT_READ_ONLY 'doctrine.readOnly';
  98.     public const HINT_INTERNAL_ITERATION 'doctrine.internal.iteration';
  99.     public const HINT_LOCK_MODE 'doctrine.lockMode';
  100.     /**
  101.      * The current state of this query.
  102.      *
  103.      * @var int
  104.      * @psalm-var self::STATE_*
  105.      */
  106.     private $_state self::STATE_DIRTY;
  107.     /**
  108.      * A snapshot of the parameter types the query was parsed with.
  109.      *
  110.      * @var array<string,Type>
  111.      */
  112.     private $parsedTypes = [];
  113.     /**
  114.      * Cached DQL query.
  115.      *
  116.      * @var string|null
  117.      */
  118.     private $dql null;
  119.     /**
  120.      * The parser result that holds DQL => SQL information.
  121.      *
  122.      * @var ParserResult
  123.      */
  124.     private $parserResult;
  125.     /**
  126.      * The first result to return (the "offset").
  127.      *
  128.      * @var int
  129.      */
  130.     private $firstResult 0;
  131.     /**
  132.      * The maximum number of results to return (the "limit").
  133.      *
  134.      * @var int|null
  135.      */
  136.     private $maxResults null;
  137.     /**
  138.      * The cache driver used for caching queries.
  139.      *
  140.      * @var CacheItemPoolInterface|null
  141.      */
  142.     private $queryCache;
  143.     /**
  144.      * Whether or not expire the query cache.
  145.      *
  146.      * @var bool
  147.      */
  148.     private $expireQueryCache false;
  149.     /**
  150.      * The query cache lifetime.
  151.      *
  152.      * @var int|null
  153.      */
  154.     private $queryCacheTTL;
  155.     /**
  156.      * Whether to use a query cache, if available. Defaults to TRUE.
  157.      *
  158.      * @var bool
  159.      */
  160.     private $useQueryCache true;
  161.     /**
  162.      * Gets the SQL query/queries that correspond to this DQL query.
  163.      *
  164.      * @return list<string>|string The built sql query or an array of all sql queries.
  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.     public function free(): void
  483.     {
  484.         parent::free();
  485.         $this->dql    null;
  486.         $this->_state self::STATE_CLEAN;
  487.     }
  488.     /**
  489.      * Sets a DQL query string.
  490.      *
  491.      * @param string|null $dqlQuery DQL Query.
  492.      */
  493.     public function setDQL($dqlQuery): self
  494.     {
  495.         if ($dqlQuery === null) {
  496.             Deprecation::trigger(
  497.                 'doctrine/orm',
  498.                 'https://github.com/doctrine/orm/pull/9784',
  499.                 'Calling %s with null is deprecated and will result in a TypeError in Doctrine 3.0',
  500.                 __METHOD__
  501.             );
  502.             return $this;
  503.         }
  504.         $this->dql    $dqlQuery;
  505.         $this->_state self::STATE_DIRTY;
  506.         return $this;
  507.     }
  508.     /**
  509.      * Returns the DQL query that is represented by this query object.
  510.      */
  511.     public function getDQL(): ?string
  512.     {
  513.         return $this->dql;
  514.     }
  515.     /**
  516.      * Returns the state of this query object
  517.      * By default the type is Doctrine_ORM_Query_Abstract::STATE_CLEAN but if it appears any unprocessed DQL
  518.      * part, it is switched to Doctrine_ORM_Query_Abstract::STATE_DIRTY.
  519.      *
  520.      * @see AbstractQuery::STATE_CLEAN
  521.      * @see AbstractQuery::STATE_DIRTY
  522.      *
  523.      * @return int The query state.
  524.      * @psalm-return self::STATE_* The query state.
  525.      */
  526.     public function getState(): int
  527.     {
  528.         return $this->_state;
  529.     }
  530.     /**
  531.      * Method to check if an arbitrary piece of DQL exists
  532.      *
  533.      * @param string $dql Arbitrary piece of DQL to check for.
  534.      */
  535.     public function contains($dql): bool
  536.     {
  537.         return stripos($this->getDQL(), $dql) !== false;
  538.     }
  539.     /**
  540.      * Sets the position of the first result to retrieve (the "offset").
  541.      *
  542.      * @param int|null $firstResult The first result to return.
  543.      *
  544.      * @return $this
  545.      */
  546.     public function setFirstResult($firstResult): self
  547.     {
  548.         if (! is_int($firstResult)) {
  549.             Deprecation::trigger(
  550.                 'doctrine/orm',
  551.                 'https://github.com/doctrine/orm/pull/9809',
  552.                 'Calling %s with %s is deprecated and will result in a TypeError in Doctrine 3.0. Pass an integer.',
  553.                 __METHOD__,
  554.                 get_debug_type($firstResult)
  555.             );
  556.             $firstResult = (int) $firstResult;
  557.         }
  558.         $this->firstResult $firstResult;
  559.         $this->_state      self::STATE_DIRTY;
  560.         return $this;
  561.     }
  562.     /**
  563.      * Gets the position of the first result the query object was set to retrieve (the "offset").
  564.      * Returns NULL if {@link setFirstResult} was not applied to this query.
  565.      *
  566.      * @return int|null The position of the first result.
  567.      */
  568.     public function getFirstResult(): ?int
  569.     {
  570.         return $this->firstResult;
  571.     }
  572.     /**
  573.      * Sets the maximum number of results to retrieve (the "limit").
  574.      *
  575.      * @param int|null $maxResults
  576.      *
  577.      * @return $this
  578.      */
  579.     public function setMaxResults($maxResults): self
  580.     {
  581.         if ($maxResults !== null) {
  582.             $maxResults = (int) $maxResults;
  583.         }
  584.         $this->maxResults $maxResults;
  585.         $this->_state     self::STATE_DIRTY;
  586.         return $this;
  587.     }
  588.     /**
  589.      * Gets the maximum number of results the query object was set to retrieve (the "limit").
  590.      * Returns NULL if {@link setMaxResults} was not applied to this query.
  591.      *
  592.      * @return int|null Maximum number of results.
  593.      */
  594.     public function getMaxResults(): ?int
  595.     {
  596.         return $this->maxResults;
  597.     }
  598.     /**
  599.      * Executes the query and returns an IterableResult that can be used to incrementally
  600.      * iterated over the result.
  601.      *
  602.      * @deprecated
  603.      *
  604.      * @param ArrayCollection|mixed[]|null $parameters    The query parameters.
  605.      * @param string|int                   $hydrationMode The hydration mode to use.
  606.      * @psalm-param ArrayCollection<int, Parameter>|array<string, mixed>|null $parameters
  607.      * @psalm-param string|AbstractQuery::HYDRATE_*|null                      $hydrationMode
  608.      */
  609.     public function iterate($parameters null$hydrationMode self::HYDRATE_OBJECT): IterableResult
  610.     {
  611.         $this->setHint(self::HINT_INTERNAL_ITERATIONtrue);
  612.         return parent::iterate($parameters$hydrationMode);
  613.     }
  614.     /** {@inheritDoc} */
  615.     public function toIterable(iterable $parameters = [], $hydrationMode self::HYDRATE_OBJECT): iterable
  616.     {
  617.         $this->setHint(self::HINT_INTERNAL_ITERATIONtrue);
  618.         return parent::toIterable($parameters$hydrationMode);
  619.     }
  620.     /**
  621.      * {@inheritdoc}
  622.      */
  623.     public function setHint($name$value): self
  624.     {
  625.         $this->_state self::STATE_DIRTY;
  626.         return parent::setHint($name$value);
  627.     }
  628.     /**
  629.      * {@inheritdoc}
  630.      */
  631.     public function setHydrationMode($hydrationMode): self
  632.     {
  633.         $this->_state self::STATE_DIRTY;
  634.         return parent::setHydrationMode($hydrationMode);
  635.     }
  636.     /**
  637.      * Set the lock mode for this Query.
  638.      *
  639.      * @see \Doctrine\DBAL\LockMode
  640.      *
  641.      * @param int $lockMode
  642.      * @psalm-param LockMode::* $lockMode
  643.      *
  644.      * @throws TransactionRequiredException
  645.      */
  646.     public function setLockMode($lockMode): self
  647.     {
  648.         if (in_array($lockMode, [LockMode::NONELockMode::PESSIMISTIC_READLockMode::PESSIMISTIC_WRITE], true)) {
  649.             if (! $this->_em->getConnection()->isTransactionActive()) {
  650.                 throw TransactionRequiredException::transactionRequired();
  651.             }
  652.         }
  653.         $this->setHint(self::HINT_LOCK_MODE$lockMode);
  654.         return $this;
  655.     }
  656.     /**
  657.      * Get the current lock mode for this query.
  658.      *
  659.      * @return int|null The current lock mode of this query or NULL if no specific lock mode is set.
  660.      */
  661.     public function getLockMode(): ?int
  662.     {
  663.         $lockMode $this->getHint(self::HINT_LOCK_MODE);
  664.         if ($lockMode === false) {
  665.             return null;
  666.         }
  667.         return $lockMode;
  668.     }
  669.     /**
  670.      * Generate a cache id for the query cache - reusing the Result-Cache-Id generator.
  671.      */
  672.     protected function getQueryCacheId(): string
  673.     {
  674.         ksort($this->_hints);
  675.         return md5(
  676.             $this->getDQL() . serialize($this->_hints) .
  677.             '&platform=' get_debug_type($this->getEntityManager()->getConnection()->getDatabasePlatform()) .
  678.             ($this->_em->hasFilters() ? $this->_em->getFilters()->getHash() : '') .
  679.             '&firstResult=' $this->firstResult '&maxResult=' $this->maxResults .
  680.             '&hydrationMode=' $this->_hydrationMode '&types=' serialize($this->parsedTypes) . 'DOCTRINE_QUERY_CACHE_SALT'
  681.         );
  682.     }
  683.     protected function getHash(): string
  684.     {
  685.         return sha1(parent::getHash() . '-' $this->firstResult '-' $this->maxResults);
  686.     }
  687.     /**
  688.      * Cleanup Query resource when clone is called.
  689.      */
  690.     public function __clone()
  691.     {
  692.         parent::__clone();
  693.         $this->_state self::STATE_DIRTY;
  694.     }
  695. }