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

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