vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php line 923

Open in your IDE?
  1. <?php
  2. /*
  3.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * This software consists of voluntary contributions made by many individuals
  16.  * and is licensed under the MIT license. For more information, see
  17.  * <http://www.doctrine-project.org>.
  18.  */
  19. namespace Doctrine\ORM;
  20. use Doctrine\Common\Persistence\Mapping\MappingException;
  21. use Doctrine\Common\Util\ClassUtils;
  22. use Doctrine\Common\Collections\Collection;
  23. use Doctrine\Common\Collections\ArrayCollection;
  24. use Doctrine\ORM\Mapping\MappingException as ORMMappingException;
  25. use Doctrine\ORM\Query\Parameter;
  26. use Doctrine\ORM\Cache\QueryCacheKey;
  27. use Doctrine\DBAL\Cache\QueryCacheProfile;
  28. /**
  29.  * Base contract for ORM queries. Base class for Query and NativeQuery.
  30.  *
  31.  * @link    www.doctrine-project.org
  32.  * @since   2.0
  33.  * @author  Benjamin Eberlei <kontakt@beberlei.de>
  34.  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
  35.  * @author  Jonathan Wage <jonwage@gmail.com>
  36.  * @author  Roman Borschel <roman@code-factory.org>
  37.  * @author  Konsta Vesterinen <kvesteri@cc.hut.fi>
  38.  */
  39. abstract class AbstractQuery
  40. {
  41.     /* Hydration mode constants */
  42.     /**
  43.      * Hydrates an object graph. This is the default behavior.
  44.      */
  45.     const HYDRATE_OBJECT 1;
  46.     /**
  47.      * Hydrates an array graph.
  48.      */
  49.     const HYDRATE_ARRAY 2;
  50.     /**
  51.      * Hydrates a flat, rectangular result set with scalar values.
  52.      */
  53.     const HYDRATE_SCALAR 3;
  54.     /**
  55.      * Hydrates a single scalar value.
  56.      */
  57.     const HYDRATE_SINGLE_SCALAR 4;
  58.     /**
  59.      * Very simple object hydrator (optimized for performance).
  60.      */
  61.     const HYDRATE_SIMPLEOBJECT 5;
  62.     /**
  63.      * The parameter map of this query.
  64.      *
  65.      * @var ArrayCollection|Parameter[]
  66.      */
  67.     protected $parameters;
  68.     /**
  69.      * The user-specified ResultSetMapping to use.
  70.      *
  71.      * @var \Doctrine\ORM\Query\ResultSetMapping
  72.      */
  73.     protected $_resultSetMapping;
  74.     /**
  75.      * The entity manager used by this query object.
  76.      *
  77.      * @var EntityManagerInterface
  78.      */
  79.     protected $_em;
  80.     /**
  81.      * The map of query hints.
  82.      *
  83.      * @var array
  84.      */
  85.     protected $_hints = [];
  86.     /**
  87.      * The hydration mode.
  88.      *
  89.      * @var string|int
  90.      */
  91.     protected $_hydrationMode self::HYDRATE_OBJECT;
  92.     /**
  93.      * @var \Doctrine\DBAL\Cache\QueryCacheProfile
  94.      */
  95.     protected $_queryCacheProfile;
  96.     /**
  97.      * Whether or not expire the result cache.
  98.      *
  99.      * @var boolean
  100.      */
  101.     protected $_expireResultCache false;
  102.     /**
  103.      * @var \Doctrine\DBAL\Cache\QueryCacheProfile
  104.      */
  105.     protected $_hydrationCacheProfile;
  106.     /**
  107.      * Whether to use second level cache, if available.
  108.      *
  109.      * @var boolean
  110.      */
  111.     protected $cacheable false;
  112.     /**
  113.      * @var boolean
  114.      */
  115.     protected $hasCache false;
  116.     /**
  117.      * Second level cache region name.
  118.      *
  119.      * @var string|null
  120.      */
  121.     protected $cacheRegion;
  122.     /**
  123.      * Second level query cache mode.
  124.      *
  125.      * @var integer|null
  126.      */
  127.     protected $cacheMode;
  128.     /**
  129.      * @var \Doctrine\ORM\Cache\Logging\CacheLogger|null
  130.      */
  131.     protected $cacheLogger;
  132.     /**
  133.      * @var integer
  134.      */
  135.     protected $lifetime 0;
  136.     /**
  137.      * Initializes a new instance of a class derived from <tt>AbstractQuery</tt>.
  138.      *
  139.      * @param \Doctrine\ORM\EntityManagerInterface $em
  140.      */
  141.     public function __construct(EntityManagerInterface $em)
  142.     {
  143.         $this->_em          $em;
  144.         $this->parameters   = new ArrayCollection();
  145.         $this->_hints       $em->getConfiguration()->getDefaultQueryHints();
  146.         $this->hasCache     $this->_em->getConfiguration()->isSecondLevelCacheEnabled();
  147.         if ($this->hasCache) {
  148.             $this->cacheLogger $em->getConfiguration()
  149.                 ->getSecondLevelCacheConfiguration()
  150.                 ->getCacheLogger();
  151.         }
  152.     }
  153.     /**
  154.      * Enable/disable second level query (result) caching for this query.
  155.      *
  156.      * @param boolean $cacheable
  157.      *
  158.      * @return static This query instance.
  159.      */
  160.     public function setCacheable($cacheable)
  161.     {
  162.         $this->cacheable = (boolean) $cacheable;
  163.         return $this;
  164.     }
  165.     /**
  166.      * @return boolean TRUE if the query results are enable for second level cache, FALSE otherwise.
  167.      */
  168.     public function isCacheable()
  169.     {
  170.         return $this->cacheable;
  171.     }
  172.     /**
  173.      * @param string $cacheRegion
  174.      *
  175.      * @return static This query instance.
  176.      */
  177.     public function setCacheRegion($cacheRegion)
  178.     {
  179.         $this->cacheRegion = (string) $cacheRegion;
  180.         return $this;
  181.     }
  182.     /**
  183.     * Obtain the name of the second level query cache region in which query results will be stored
  184.     *
  185.     * @return string|null The cache region name; NULL indicates the default region.
  186.     */
  187.     public function getCacheRegion()
  188.     {
  189.         return $this->cacheRegion;
  190.     }
  191.     /**
  192.      * @return boolean TRUE if the query cache and second level cache are enabled, FALSE otherwise.
  193.      */
  194.     protected function isCacheEnabled()
  195.     {
  196.         return $this->cacheable && $this->hasCache;
  197.     }
  198.     /**
  199.      * @return integer
  200.      */
  201.     public function getLifetime()
  202.     {
  203.         return $this->lifetime;
  204.     }
  205.     /**
  206.      * Sets the life-time for this query into second level cache.
  207.      *
  208.      * @param integer $lifetime
  209.      *
  210.      * @return \Doctrine\ORM\AbstractQuery This query instance.
  211.      */
  212.     public function setLifetime($lifetime)
  213.     {
  214.         $this->lifetime = (integer) $lifetime;
  215.         return $this;
  216.     }
  217.     /**
  218.      * @return integer
  219.      */
  220.     public function getCacheMode()
  221.     {
  222.         return $this->cacheMode;
  223.     }
  224.     /**
  225.      * @param integer $cacheMode
  226.      *
  227.      * @return \Doctrine\ORM\AbstractQuery This query instance.
  228.      */
  229.     public function setCacheMode($cacheMode)
  230.     {
  231.         $this->cacheMode = (integer) $cacheMode;
  232.         return $this;
  233.     }
  234.     /**
  235.      * Gets the SQL query that corresponds to this query object.
  236.      * The returned SQL syntax depends on the connection driver that is used
  237.      * by this query object at the time of this method call.
  238.      *
  239.      * @return string SQL query
  240.      */
  241.     abstract public function getSQL();
  242.     /**
  243.      * Retrieves the associated EntityManager of this Query instance.
  244.      *
  245.      * @return \Doctrine\ORM\EntityManager
  246.      */
  247.     public function getEntityManager()
  248.     {
  249.         return $this->_em;
  250.     }
  251.     /**
  252.      * Frees the resources used by the query object.
  253.      *
  254.      * Resets Parameters, Parameter Types and Query Hints.
  255.      *
  256.      * @return void
  257.      */
  258.     public function free()
  259.     {
  260.         $this->parameters = new ArrayCollection();
  261.         $this->_hints $this->_em->getConfiguration()->getDefaultQueryHints();
  262.     }
  263.     /**
  264.      * Get all defined parameters.
  265.      *
  266.      * @return ArrayCollection The defined query parameters.
  267.      */
  268.     public function getParameters()
  269.     {
  270.         return $this->parameters;
  271.     }
  272.     /**
  273.      * Gets a query parameter.
  274.      *
  275.      * @param mixed $key The key (index or name) of the bound parameter.
  276.      *
  277.      * @return Query\Parameter|null The value of the bound parameter, or NULL if not available.
  278.      */
  279.     public function getParameter($key)
  280.     {
  281.         $filteredParameters $this->parameters->filter(
  282.             function (Query\Parameter $parameter) use ($key) : bool {
  283.                 $parameterName $parameter->getName();
  284.                 return $key === $parameterName || (string) $key === (string) $parameterName;
  285.             }
  286.         );
  287.         return ! $filteredParameters->isEmpty() ? $filteredParameters->first() : null;
  288.     }
  289.     /**
  290.      * Sets a collection of query parameters.
  291.      *
  292.      * @param ArrayCollection|mixed[] $parameters
  293.      *
  294.      * @return static This query instance.
  295.      */
  296.     public function setParameters($parameters)
  297.     {
  298.         // BC compatibility with 2.3-
  299.         if (is_array($parameters)) {
  300.             $parameterCollection = new ArrayCollection();
  301.             foreach ($parameters as $key => $value) {
  302.                 $parameterCollection->add(new Parameter($key$value));
  303.             }
  304.             $parameters $parameterCollection;
  305.         }
  306.         $this->parameters $parameters;
  307.         return $this;
  308.     }
  309.     /**
  310.      * Sets a query parameter.
  311.      *
  312.      * @param string|int  $key   The parameter position or name.
  313.      * @param mixed       $value The parameter value.
  314.      * @param string|null $type  The parameter type. If specified, the given value will be run through
  315.      *                           the type conversion of this type. This is usually not needed for
  316.      *                           strings and numeric types.
  317.      *
  318.      * @return static This query instance.
  319.      */
  320.     public function setParameter($key$value$type null)
  321.     {
  322.         $existingParameter $this->getParameter($key);
  323.         if ($existingParameter !== null) {
  324.             $existingParameter->setValue($value$type);
  325.             return $this;
  326.         }
  327.         $this->parameters->add(new Parameter($key$value$type));
  328.         return $this;
  329.     }
  330.     /**
  331.      * Processes an individual parameter value.
  332.      *
  333.      * @param mixed $value
  334.      *
  335.      * @return array|string
  336.      *
  337.      * @throws \Doctrine\ORM\ORMInvalidArgumentException
  338.      */
  339.     public function processParameterValue($value)
  340.     {
  341.         if (is_scalar($value)) {
  342.             return $value;
  343.         }
  344.         if ($value instanceof Collection) {
  345.             $value $value->toArray();
  346.         }
  347.         if (is_array($value)) {
  348.             foreach ($value as $key => $paramValue) {
  349.                 $paramValue  $this->processParameterValue($paramValue);
  350.                 $value[$key] = is_array($paramValue) ? reset($paramValue) : $paramValue;
  351.             }
  352.             return $value;
  353.         }
  354.         if ($value instanceof Mapping\ClassMetadata) {
  355.             return $value->name;
  356.         }
  357.         if (! is_object($value)) {
  358.             return $value;
  359.         }
  360.         try {
  361.             $value $this->_em->getUnitOfWork()->getSingleIdentifierValue($value);
  362.             if ($value === null) {
  363.                 throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
  364.             }
  365.         } catch (MappingException ORMMappingException $e) {
  366.             // Silence any mapping exceptions. These can occur if the object in
  367.             // question is not a mapped entity, in which case we just don't do
  368.             // any preparation on the value.
  369.         }
  370.         return $value;
  371.     }
  372.     /**
  373.      * Sets the ResultSetMapping that should be used for hydration.
  374.      *
  375.      * @param \Doctrine\ORM\Query\ResultSetMapping $rsm
  376.      *
  377.      * @return static This query instance.
  378.      */
  379.     public function setResultSetMapping(Query\ResultSetMapping $rsm)
  380.     {
  381.         $this->translateNamespaces($rsm);
  382.         $this->_resultSetMapping $rsm;
  383.         return $this;
  384.     }
  385.     /**
  386.      * Gets the ResultSetMapping used for hydration.
  387.      *
  388.      * @return \Doctrine\ORM\Query\ResultSetMapping
  389.      */
  390.     protected function getResultSetMapping()
  391.     {
  392.         return $this->_resultSetMapping;
  393.     }
  394.     /**
  395.      * Allows to translate entity namespaces to full qualified names.
  396.      *
  397.      * @param Query\ResultSetMapping $rsm
  398.      *
  399.      * @return void
  400.      */
  401.     private function translateNamespaces(Query\ResultSetMapping $rsm)
  402.     {
  403.         $translate = function ($alias) {
  404.             return $this->_em->getClassMetadata($alias)->getName();
  405.         };
  406.         $rsm->aliasMap array_map($translate$rsm->aliasMap);
  407.         $rsm->declaringClasses array_map($translate$rsm->declaringClasses);
  408.     }
  409.     /**
  410.      * Set a cache profile for hydration caching.
  411.      *
  412.      * If no result cache driver is set in the QueryCacheProfile, the default
  413.      * result cache driver is used from the configuration.
  414.      *
  415.      * Important: Hydration caching does NOT register entities in the
  416.      * UnitOfWork when retrieved from the cache. Never use result cached
  417.      * entities for requests that also flush the EntityManager. If you want
  418.      * some form of caching with UnitOfWork registration you should use
  419.      * {@see AbstractQuery::setResultCacheProfile()}.
  420.      *
  421.      * @example
  422.      * $lifetime = 100;
  423.      * $resultKey = "abc";
  424.      * $query->setHydrationCacheProfile(new QueryCacheProfile());
  425.      * $query->setHydrationCacheProfile(new QueryCacheProfile($lifetime, $resultKey));
  426.      *
  427.      * @param \Doctrine\DBAL\Cache\QueryCacheProfile $profile
  428.      *
  429.      * @return static This query instance.
  430.      */
  431.     public function setHydrationCacheProfile(QueryCacheProfile $profile null)
  432.     {
  433.         if ($profile !== null && ! $profile->getResultCacheDriver()) {
  434.             $resultCacheDriver $this->_em->getConfiguration()->getHydrationCacheImpl();
  435.             $profile $profile->setResultCacheDriver($resultCacheDriver);
  436.         }
  437.         $this->_hydrationCacheProfile $profile;
  438.         return $this;
  439.     }
  440.     /**
  441.      * @return \Doctrine\DBAL\Cache\QueryCacheProfile
  442.      */
  443.     public function getHydrationCacheProfile()
  444.     {
  445.         return $this->_hydrationCacheProfile;
  446.     }
  447.     /**
  448.      * Set a cache profile for the result cache.
  449.      *
  450.      * If no result cache driver is set in the QueryCacheProfile, the default
  451.      * result cache driver is used from the configuration.
  452.      *
  453.      * @param \Doctrine\DBAL\Cache\QueryCacheProfile $profile
  454.      *
  455.      * @return static This query instance.
  456.      */
  457.     public function setResultCacheProfile(QueryCacheProfile $profile null)
  458.     {
  459.         if ($profile !== null && ! $profile->getResultCacheDriver()) {
  460.             $resultCacheDriver $this->_em->getConfiguration()->getResultCacheImpl();
  461.             $profile $profile->setResultCacheDriver($resultCacheDriver);
  462.         }
  463.         $this->_queryCacheProfile $profile;
  464.         return $this;
  465.     }
  466.     /**
  467.      * Defines a cache driver to be used for caching result sets and implicitly enables caching.
  468.      *
  469.      * @param \Doctrine\Common\Cache\Cache|null $resultCacheDriver Cache driver
  470.      *
  471.      * @return static This query instance.
  472.      *
  473.      * @throws ORMException
  474.      */
  475.     public function setResultCacheDriver($resultCacheDriver null)
  476.     {
  477.         if ($resultCacheDriver !== null && ! ($resultCacheDriver instanceof \Doctrine\Common\Cache\Cache)) {
  478.             throw ORMException::invalidResultCacheDriver();
  479.         }
  480.         $this->_queryCacheProfile $this->_queryCacheProfile
  481.             $this->_queryCacheProfile->setResultCacheDriver($resultCacheDriver)
  482.             : new QueryCacheProfile(0null$resultCacheDriver);
  483.         return $this;
  484.     }
  485.     /**
  486.      * Returns the cache driver used for caching result sets.
  487.      *
  488.      * @deprecated
  489.      *
  490.      * @return \Doctrine\Common\Cache\Cache Cache driver
  491.      */
  492.     public function getResultCacheDriver()
  493.     {
  494.         if ($this->_queryCacheProfile && $this->_queryCacheProfile->getResultCacheDriver()) {
  495.             return $this->_queryCacheProfile->getResultCacheDriver();
  496.         }
  497.         return $this->_em->getConfiguration()->getResultCacheImpl();
  498.     }
  499.     /**
  500.      * Set whether or not to cache the results of this query and if so, for
  501.      * how long and which ID to use for the cache entry.
  502.      *
  503.      * @param boolean $bool
  504.      * @param integer $lifetime
  505.      * @param string  $resultCacheId
  506.      *
  507.      * @return static This query instance.
  508.      */
  509.     public function useResultCache($bool$lifetime null$resultCacheId null)
  510.     {
  511.         if ($bool) {
  512.             $this->setResultCacheLifetime($lifetime);
  513.             $this->setResultCacheId($resultCacheId);
  514.             return $this;
  515.         }
  516.         $this->_queryCacheProfile null;
  517.         return $this;
  518.     }
  519.     /**
  520.      * Defines how long the result cache will be active before expire.
  521.      *
  522.      * @param integer $lifetime How long the cache entry is valid.
  523.      *
  524.      * @return static This query instance.
  525.      */
  526.     public function setResultCacheLifetime($lifetime)
  527.     {
  528.         $lifetime = ($lifetime !== null) ? (int) $lifetime 0;
  529.         $this->_queryCacheProfile $this->_queryCacheProfile
  530.             $this->_queryCacheProfile->setLifetime($lifetime)
  531.             : new QueryCacheProfile($lifetimenull$this->_em->getConfiguration()->getResultCacheImpl());
  532.         return $this;
  533.     }
  534.     /**
  535.      * Retrieves the lifetime of resultset cache.
  536.      *
  537.      * @deprecated
  538.      *
  539.      * @return integer
  540.      */
  541.     public function getResultCacheLifetime()
  542.     {
  543.         return $this->_queryCacheProfile $this->_queryCacheProfile->getLifetime() : 0;
  544.     }
  545.     /**
  546.      * Defines if the result cache is active or not.
  547.      *
  548.      * @param boolean $expire Whether or not to force resultset cache expiration.
  549.      *
  550.      * @return static This query instance.
  551.      */
  552.     public function expireResultCache($expire true)
  553.     {
  554.         $this->_expireResultCache $expire;
  555.         return $this;
  556.     }
  557.     /**
  558.      * Retrieves if the resultset cache is active or not.
  559.      *
  560.      * @return boolean
  561.      */
  562.     public function getExpireResultCache()
  563.     {
  564.         return $this->_expireResultCache;
  565.     }
  566.     /**
  567.      * @return QueryCacheProfile
  568.      */
  569.     public function getQueryCacheProfile()
  570.     {
  571.         return $this->_queryCacheProfile;
  572.     }
  573.     /**
  574.      * Change the default fetch mode of an association for this query.
  575.      *
  576.      * $fetchMode can be one of ClassMetadata::FETCH_EAGER or ClassMetadata::FETCH_LAZY
  577.      *
  578.      * @param string $class
  579.      * @param string $assocName
  580.      * @param int    $fetchMode
  581.      *
  582.      * @return static This query instance.
  583.      */
  584.     public function setFetchMode($class$assocName$fetchMode)
  585.     {
  586.         if ($fetchMode !== Mapping\ClassMetadata::FETCH_EAGER) {
  587.             $fetchMode Mapping\ClassMetadata::FETCH_LAZY;
  588.         }
  589.         $this->_hints['fetchMode'][$class][$assocName] = $fetchMode;
  590.         return $this;
  591.     }
  592.     /**
  593.      * Defines the processing mode to be used during hydration / result set transformation.
  594.      *
  595.      * @param string|int $hydrationMode Doctrine processing mode to be used during hydration process.
  596.      *                                  One of the Query::HYDRATE_* constants.
  597.      *
  598.      * @return static This query instance.
  599.      */
  600.     public function setHydrationMode($hydrationMode)
  601.     {
  602.         $this->_hydrationMode $hydrationMode;
  603.         return $this;
  604.     }
  605.     /**
  606.      * Gets the hydration mode currently used by the query.
  607.      *
  608.      * @return string|int
  609.      */
  610.     public function getHydrationMode()
  611.     {
  612.         return $this->_hydrationMode;
  613.     }
  614.     /**
  615.      * Gets the list of results for the query.
  616.      *
  617.      * Alias for execute(null, $hydrationMode = HYDRATE_OBJECT).
  618.      *
  619.      * @param string|int $hydrationMode
  620.      *
  621.      * @return mixed
  622.      */
  623.     public function getResult($hydrationMode self::HYDRATE_OBJECT)
  624.     {
  625.         return $this->execute(null$hydrationMode);
  626.     }
  627.     /**
  628.      * Gets the array of results for the query.
  629.      *
  630.      * Alias for execute(null, HYDRATE_ARRAY).
  631.      *
  632.      * @return array
  633.      */
  634.     public function getArrayResult()
  635.     {
  636.         return $this->execute(nullself::HYDRATE_ARRAY);
  637.     }
  638.     /**
  639.      * Gets the scalar results for the query.
  640.      *
  641.      * Alias for execute(null, HYDRATE_SCALAR).
  642.      *
  643.      * @return array
  644.      */
  645.     public function getScalarResult()
  646.     {
  647.         return $this->execute(nullself::HYDRATE_SCALAR);
  648.     }
  649.     /**
  650.      * Get exactly one result or null.
  651.      *
  652.      * @param string|int $hydrationMode
  653.      *
  654.      * @return mixed
  655.      *
  656.      * @throws NonUniqueResultException
  657.      */
  658.     public function getOneOrNullResult($hydrationMode null)
  659.     {
  660.         try {
  661.             $result $this->execute(null$hydrationMode);
  662.         } catch (NoResultException $e) {
  663.             return null;
  664.         }
  665.         if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && ! $result) {
  666.             return null;
  667.         }
  668.         if ( ! is_array($result)) {
  669.             return $result;
  670.         }
  671.         if (count($result) > 1) {
  672.             throw new NonUniqueResultException;
  673.         }
  674.         return array_shift($result);
  675.     }
  676.     /**
  677.      * Gets the single result of the query.
  678.      *
  679.      * Enforces the presence as well as the uniqueness of the result.
  680.      *
  681.      * If the result is not unique, a NonUniqueResultException is thrown.
  682.      * If there is no result, a NoResultException is thrown.
  683.      *
  684.      * @param string|int $hydrationMode
  685.      *
  686.      * @return mixed
  687.      *
  688.      * @throws NonUniqueResultException If the query result is not unique.
  689.      * @throws NoResultException        If the query returned no result and hydration mode is not HYDRATE_SINGLE_SCALAR.
  690.      */
  691.     public function getSingleResult($hydrationMode null)
  692.     {
  693.         $result $this->execute(null$hydrationMode);
  694.         if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && ! $result) {
  695.             throw new NoResultException;
  696.         }
  697.         if ( ! is_array($result)) {
  698.             return $result;
  699.         }
  700.         if (count($result) > 1) {
  701.             throw new NonUniqueResultException;
  702.         }
  703.         return array_shift($result);
  704.     }
  705.     /**
  706.      * Gets the single scalar result of the query.
  707.      *
  708.      * Alias for getSingleResult(HYDRATE_SINGLE_SCALAR).
  709.      *
  710.      * @return mixed The scalar result.
  711.      *
  712.      * @throws NoResultException        If the query returned no result.
  713.      * @throws NonUniqueResultException If the query result is not unique.
  714.      */
  715.     public function getSingleScalarResult()
  716.     {
  717.         return $this->getSingleResult(self::HYDRATE_SINGLE_SCALAR);
  718.     }
  719.     /**
  720.      * Sets a query hint. If the hint name is not recognized, it is silently ignored.
  721.      *
  722.      * @param string $name  The name of the hint.
  723.      * @param mixed  $value The value of the hint.
  724.      *
  725.      * @return static This query instance.
  726.      */
  727.     public function setHint($name$value)
  728.     {
  729.         $this->_hints[$name] = $value;
  730.         return $this;
  731.     }
  732.     /**
  733.      * Gets the value of a query hint. If the hint name is not recognized, FALSE is returned.
  734.      *
  735.      * @param string $name The name of the hint.
  736.      *
  737.      * @return mixed The value of the hint or FALSE, if the hint name is not recognized.
  738.      */
  739.     public function getHint($name)
  740.     {
  741.         return isset($this->_hints[$name]) ? $this->_hints[$name] : false;
  742.     }
  743.     /**
  744.      * Check if the query has a hint
  745.      *
  746.      * @param string $name The name of the hint
  747.      *
  748.      * @return bool False if the query does not have any hint
  749.      */
  750.     public function hasHint($name)
  751.     {
  752.         return isset($this->_hints[$name]);
  753.     }
  754.     /**
  755.      * Return the key value map of query hints that are currently set.
  756.      *
  757.      * @return array
  758.      */
  759.     public function getHints()
  760.     {
  761.         return $this->_hints;
  762.     }
  763.     /**
  764.      * Executes the query and returns an IterableResult that can be used to incrementally
  765.      * iterate over the result.
  766.      *
  767.      * @param ArrayCollection|array|null $parameters    The query parameters.
  768.      * @param string|int|null            $hydrationMode The hydration mode to use.
  769.      *
  770.      * @return \Doctrine\ORM\Internal\Hydration\IterableResult
  771.      */
  772.     public function iterate($parameters null$hydrationMode null)
  773.     {
  774.         if ($hydrationMode !== null) {
  775.             $this->setHydrationMode($hydrationMode);
  776.         }
  777.         if ( ! empty($parameters)) {
  778.             $this->setParameters($parameters);
  779.         }
  780.         $rsm  $this->getResultSetMapping();
  781.         $stmt $this->_doExecute();
  782.         return $this->_em->newHydrator($this->_hydrationMode)->iterate($stmt$rsm$this->_hints);
  783.     }
  784.     /**
  785.      * Executes the query.
  786.      *
  787.      * @param ArrayCollection|array|null $parameters Query parameters.
  788.      * @param string|int|null            $hydrationMode Processing mode to be used during the hydration process.
  789.      *
  790.      * @return mixed
  791.      */
  792.     public function execute($parameters null$hydrationMode null)
  793.     {
  794.         if ($this->cacheable && $this->isCacheEnabled()) {
  795.             return $this->executeUsingQueryCache($parameters$hydrationMode);
  796.         }
  797.         return $this->executeIgnoreQueryCache($parameters$hydrationMode);
  798.     }
  799.     /**
  800.      * Execute query ignoring second level cache.
  801.      *
  802.      * @param ArrayCollection|array|null $parameters
  803.      * @param string|int|null            $hydrationMode
  804.      *
  805.      * @return mixed
  806.      */
  807.     private function executeIgnoreQueryCache($parameters null$hydrationMode null)
  808.     {
  809.         if ($hydrationMode !== null) {
  810.             $this->setHydrationMode($hydrationMode);
  811.         }
  812.         if ( ! empty($parameters)) {
  813.             $this->setParameters($parameters);
  814.         }
  815.         $setCacheEntry = function() {};
  816.         if ($this->_hydrationCacheProfile !== null) {
  817.             list($cacheKey$realCacheKey) = $this->getHydrationCacheId();
  818.             $queryCacheProfile $this->getHydrationCacheProfile();
  819.             $cache             $queryCacheProfile->getResultCacheDriver();
  820.             $result            $cache->fetch($cacheKey);
  821.             if (isset($result[$realCacheKey])) {
  822.                 return $result[$realCacheKey];
  823.             }
  824.             if ( ! $result) {
  825.                 $result = [];
  826.             }
  827.             $setCacheEntry = function($data) use ($cache$result$cacheKey$realCacheKey$queryCacheProfile) {
  828.                 $result[$realCacheKey] = $data;
  829.                 $cache->save($cacheKey$result$queryCacheProfile->getLifetime());
  830.             };
  831.         }
  832.         $stmt $this->_doExecute();
  833.         if (is_numeric($stmt)) {
  834.             $setCacheEntry($stmt);
  835.             return $stmt;
  836.         }
  837.         $rsm  $this->getResultSetMapping();
  838.         $data $this->_em->newHydrator($this->_hydrationMode)->hydrateAll($stmt$rsm$this->_hints);
  839.         $setCacheEntry($data);
  840.         return $data;
  841.     }
  842.     /**
  843.      * Load from second level cache or executes the query and put into cache.
  844.      *
  845.      * @param ArrayCollection|array|null $parameters
  846.      * @param string|int|null            $hydrationMode
  847.      *
  848.      * @return mixed
  849.      */
  850.     private function executeUsingQueryCache($parameters null$hydrationMode null)
  851.     {
  852.         $rsm        $this->getResultSetMapping();
  853.         $queryCache $this->_em->getCache()->getQueryCache($this->cacheRegion);
  854.         $queryKey   = new QueryCacheKey(
  855.             $this->getHash(),
  856.             $this->lifetime,
  857.             $this->cacheMode ?: Cache::MODE_NORMAL,
  858.             $this->getTimestampKey()
  859.         );
  860.         $result     $queryCache->get($queryKey$rsm$this->_hints);
  861.         if ($result !== null) {
  862.             if ($this->cacheLogger) {
  863.                 $this->cacheLogger->queryCacheHit($queryCache->getRegion()->getName(), $queryKey);
  864.             }
  865.             return $result;
  866.         }
  867.         $result $this->executeIgnoreQueryCache($parameters$hydrationMode);
  868.         $cached $queryCache->put($queryKey$rsm$result$this->_hints);
  869.         if ($this->cacheLogger) {
  870.             $this->cacheLogger->queryCacheMiss($queryCache->getRegion()->getName(), $queryKey);
  871.             if ($cached) {
  872.                 $this->cacheLogger->queryCachePut($queryCache->getRegion()->getName(), $queryKey);
  873.             }
  874.         }
  875.         return $result;
  876.     }
  877.     /**
  878.      * @return \Doctrine\ORM\Cache\TimestampCacheKey|null
  879.      */
  880.     private function getTimestampKey()
  881.     {
  882.         $entityName reset($this->_resultSetMapping->aliasMap);
  883.         if (empty($entityName)) {
  884.             return null;
  885.         }
  886.         $metadata $this->_em->getClassMetadata($entityName);
  887.         return new Cache\TimestampCacheKey($metadata->rootEntityName);
  888.     }
  889.     /**
  890.      * Get the result cache id to use to store the result set cache entry.
  891.      * Will return the configured id if it exists otherwise a hash will be
  892.      * automatically generated for you.
  893.      *
  894.      * @return array ($key, $hash)
  895.      */
  896.     protected function getHydrationCacheId()
  897.     {
  898.         $parameters = [];
  899.         foreach ($this->getParameters() as $parameter) {
  900.             $parameters[$parameter->getName()] = $this->processParameterValue($parameter->getValue());
  901.         }
  902.         $sql                    $this->getSQL();
  903.         $queryCacheProfile      $this->getHydrationCacheProfile();
  904.         $hints                  $this->getHints();
  905.         $hints['hydrationMode'] = $this->getHydrationMode();
  906.         ksort($hints);
  907.         return $queryCacheProfile->generateCacheKeys($sql$parameters$hints);
  908.     }
  909.     /**
  910.      * Set the result cache id to use to store the result set cache entry.
  911.      * If this is not explicitly set by the developer then a hash is automatically
  912.      * generated for you.
  913.      *
  914.      * @param string $id
  915.      *
  916.      * @return static This query instance.
  917.      */
  918.     public function setResultCacheId($id)
  919.     {
  920.         $this->_queryCacheProfile $this->_queryCacheProfile
  921.             $this->_queryCacheProfile->setCacheKey($id)
  922.             : new QueryCacheProfile(0$id$this->_em->getConfiguration()->getResultCacheImpl());
  923.         return $this;
  924.     }
  925.     /**
  926.      * Get the result cache id to use to store the result set cache entry if set.
  927.      *
  928.      * @deprecated
  929.      *
  930.      * @return string
  931.      */
  932.     public function getResultCacheId()
  933.     {
  934.         return $this->_queryCacheProfile $this->_queryCacheProfile->getCacheKey() : null;
  935.     }
  936.     /**
  937.      * Executes the query and returns a the resulting Statement object.
  938.      *
  939.      * @return \Doctrine\DBAL\Driver\Statement The executed database statement that holds the results.
  940.      */
  941.     abstract protected function _doExecute();
  942.     /**
  943.      * Cleanup Query resource when clone is called.
  944.      *
  945.      * @return void
  946.      */
  947.     public function __clone()
  948.     {
  949.         $this->parameters = new ArrayCollection();
  950.         $this->_hints = [];
  951.         $this->_hints $this->_em->getConfiguration()->getDefaultQueryHints();
  952.     }
  953.     /**
  954.      * Generates a string of currently query to use for the cache second level cache.
  955.      *
  956.      * @return string
  957.      */
  958.     protected function getHash()
  959.     {
  960.         $query  $this->getSQL();
  961.         $hints  $this->getHints();
  962.         $params array_map(function(Parameter $parameter) {
  963.             // Small optimization
  964.             // Does not invoke processParameterValue for scalar values
  965.             if (is_scalar($value $parameter->getValue())) {
  966.                 return $value;
  967.             }
  968.             return $this->processParameterValue($value);
  969.         }, $this->parameters->getValues());
  970.         ksort($hints);
  971.         return sha1($query '-' serialize($params) . '-' serialize($hints));
  972.     }
  973. }