PluginProbe ʕ •ᴥ•ʔ
MailPoet – Newsletters, Email Marketing, and Automation / 5.36.0
MailPoet – Newsletters, Email Marketing, and Automation v5.36.0
5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 4.44.0 4.44.1 4.45.0 4.46.0 4.47.0 4.48.0 4.48.1 4.48.2 4.49.0 4.49.1 4.5.0 4.5.1 4.5.2 4.50.0 4.50.1 4.51.0 4.51.1 4.51.2 4.52.0 4.53.0 4.54.0 4.55.0 4.56.0 4.57.0 4.58.0 4.58.1 4.58.2 4.6.0 4.6.1 4.6.2 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 5.0.0 5.0.1 5.0.2 5.1.0 5.1.1 5.10.0 5.10.1 5.11.0 5.12.0 5.12.1 5.12.10 5.12.11 5.12.12 5.12.13 5.12.2 5.12.3 5.12.4 5.12.5 5.12.6 5.12.7 5.12.8 5.12.9 5.13.0 5.13.1 5.13.2 5.14.0 5.14.1 5.14.2 5.14.3 5.15.0 5.15.1 5.16.0 5.16.1 5.16.2 5.16.3 5.16.4 5.17.0 5.17.1 5.17.2 5.17.3 5.17.4 5.17.5 5.17.6 5.18.0 5.19.0 5.2.0 5.2.1 5.2.2 5.2.3 5.20.0 5.21.0 5.21.1 5.21.2 5.21.3 5.22.0 5.22.1 5.22.2 5.22.3 5.22.4 5.23.0 5.23.1 5.23.2 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.3.7 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 5.5.2 5.6.0 5.6.1 5.6.2 5.6.3 5.6.4 5.7.0 5.7.1 5.8.0 5.8.1 5.9.0 3.0.0-beta.15 3.7.1 3.0.0-beta.16 3.7.2 3.0.0-beta.17 3.7.3 3.0.0-beta.18 3.7.4 3.0.0-beta.19 3.7.5 3.0.0-beta.2 3.7.6 3.0.0-beta.20 3.7.8 3.0.0-beta.21 3.70.0 3.0.0-beta.22 3.71.0 3.0.0-beta.23 3.71.1 3.0.0-beta.23.1 3.71.2 3.0.0-beta.23.2 3.71.3 3.0.0-beta.24 3.72.0 3.0.0-beta.25 3.73.0 3.0.0-beta.26 3.73.1 3.0.0-beta.27 3.73.2 3.0.0-beta.28 3.74.0 3.0.0-beta.29 3.74.1 3.0.0-beta.3 3.74.2 3.0.0-beta.30 3.74.3 3.0.0-beta.31 3.75.0 3.0.0-beta.32 3.75.1 3.0.0-beta.33 3.76.0 3.0.0-beta.33.1 3.77.0 3.0.0-beta.34.0.0 3.77.1 3.0.0-beta.36.0.0 3.78.0 3.0.0-beta.36.0.1 3.79.0 3.0.0-beta.36.2.0 3.8 3.0.0-beta.36.3.0 3.8.1 3.0.0-beta.36.3.1 3.8.2 3.0.0-beta.37.0.0 3.8.3 3.0.0-beta.4 3.8.4 3.0.0-beta.5 3.8.5 3.0.0-beta.6 3.8.6 3.0.0-beta.7 3.80.0 3.0.0-beta.7.1 3.81.0 3.0.0-beta.8 3.82.0 3.0.0-beta.9 3.83.0 3.0.0-rc.1.0.0 3.84.0 3.0.0-rc.1.0.1 3.84.1 3.0.0-rc.1.0.2 3.85.0 3.0.0-rc.1.0.3 3.85.1 3.0.0-rc.1.0.4 3.86.0 3.0.0-rc.2.0.0 3.87.0 3.0.0-rc.2.0.1 3.87.1 3.0.0-rc.2.0.2 3.87.2 3.0.0-rc.2.0.3 3.88.0 3.0.1 3.88.1 3.0.2 3.88.2 3.0.3 3.89.0 3.0.4 3.89.1 3.0.5 3.89.2 3.0.6 3.89.3 3.0.7 3.89.4 3.0.8 3.9.0 3.0.9 3.9.1 3.1.0 3.90.0 3.10 3.90.1 3.10.1 3.90.2 3.100.0 3.91.0 3.100.1 3.91.1 3.100.2 3.92.0 3.101.0 3.92.1 3.101.1 3.93.0 3.102.0 3.93.1 3.102.1 3.94.0 3.103.0 3.95.0 3.103.1 3.95.1 3.11.0 3.96.0 3.11.1 3.96.1 3.11.2 3.97.0 3.11.3 3.98.0 3.11.4 3.98.1 3.11.5 3.99.0 3.12.0 3.99.1 3.12.1 4.0.0 3.13.0 4.0.1 3.14.0 4.1.0 3.14.1 4.1.1 3.15.0 4.10.0 3.16.0 4.11.0 3.16.1 4.11.1 3.16.2 4.12.0 3.16.3 4.12.1 3.17.0 4.12.2 3.17.1 4.13.0 3.17.2 4.14.0 3.18.0 4.15.0 3.18.1 4.16.0 3.18.2 4.17.0 3.19.0 4.17.1 3.19.1 4.18.0 3.19.2 4.18.1 3.19.3 4.19.0 3.2.0 4.2.0 3.2.1 4.20.0 3.2.2 4.20.1 3.2.3 4.20.2 3.2.4 4.21.0 3.2.5 4.22.0 3.20.0 4.22.1 3.21.0 4.22.2 3.21.1 4.23.0 3.22.0 4.24.0 3.23.0 4.25.0 3.23.1 4.26.0 3.23.2 4.26.1 3.24.0 4.27.0 3.25.0 4.28.0 3.25.1 4.29.0 3.26.0 4.3.0 3.26.1 4.3.1 3.27.0 4.30.0 3.28.0 4.31.0 3.29.0 4.31.1 3.3.0 4.32.0 3.3.1 4.33.0 3.3.2 4.34.0 3.3.3 4.35.0 3.3.4 4.35.1 3.3.5 4.36.0 3.3.6 4.37.0 3.30.0 4.38.0 3.31.0 4.39.0 3.31.1 4.4.0 3.32.0 4.40.0 3.32.1 4.41.0 3.32.2 4.41.1 3.33.0 4.41.2 3.34.0 4.41.3 3.34.1 4.42.0 3.34.2 4.42.1 3.34.3 3.34.4 3.35.0 3.35.1 3.35.3 3.35.4 3.36.0 3.37.0 3.37.1 3.37.2 3.37.3 3.38.0 3.38.1 3.39.0 3.39.1 3.39.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.40.0 3.40.1 3.41.0 3.41.1 3.41.2 3.42.0 3.42.1 3.42.2 3.42.3 3.43.0 3.43.1 3.44.0 3.45.0 3.45.1 3.46.0 3.46.1 3.46.10 3.46.11 3.46.12 3.46.13 3.46.14 3.46.2 3.46.3 3.46.4 3.46.5 3.46.6 3.46.7 3.46.8 3.46.9 3.47.0 3.47.1 3.47.10 3.47.11 3.47.2 3.47.3 3.47.5 3.47.6 3.47.7 3.47.9 3.48.0 3.48.1 3.49.0 3.49.1 3.5.0 3.5.1 3.50.0 3.51.0 3.51.1 3.51.2 3.52.0 3.53.0 3.54.0 3.54.1 3.54.2 3.54.3 3.55.0 3.55.1 3.56.0 3.56.1 3.56.2 3.57.0 3.57.1 3.58.0 3.59.0 3.59.1 3.59.2 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.60.0 3.60.1 3.60.10 3.60.11 3.60.12 3.60.2 3.60.3 3.60.4 3.60.6 3.60.7 3.60.8 3.60.9 3.61.0 3.62.0 3.62.1 3.63.0 3.64.0 3.64.1 3.64.2 3.64.3 3.65.0 trunk 3.65.1 3.0.0 3.66.0 3.0.0-beta.1 3.67.0 3.0.0-beta.10 3.67.1 3.0.0-beta.11 3.68.0 3.0.0-beta.12 3.69.0 3.0.0-beta.13 3.69.1 3.0.0-beta.14 3.7.0
mailpoet / vendor-prefixed / doctrine / orm / src / AbstractQuery.php
mailpoet / vendor-prefixed / doctrine / orm / src Last commit date
Cache 9 months ago Decorator 9 months ago Event 9 months ago Exception 9 months ago Id 9 months ago Internal 9 months ago Mapping 9 months ago Persisters 8 months ago Proxy 9 months ago Query 9 months ago Repository 9 months ago Tools 9 months ago Utility 9 months ago AbstractQuery.php 9 months ago Cache.php 9 months ago Configuration.php 9 months ago EntityManager.php 9 months ago EntityManagerInterface.php 9 months ago EntityNotFoundException.php 9 months ago EntityRepository.php 9 months ago Events.php 9 months ago LazyCriteriaCollection.php 9 months ago NativeQuery.php 9 months ago NoResultException.php 9 months ago NonUniqueResultException.php 9 months ago ORMException.php 9 months ago ORMInvalidArgumentException.php 9 months ago ORMSetup.php 9 months ago OptimisticLockException.php 9 months ago PersistentCollection.php 9 months ago PessimisticLockException.php 9 months ago Query.php 9 months ago QueryBuilder.php 9 months ago TransactionRequiredException.php 9 months ago UnexpectedResultException.php 9 months ago UnitOfWork.php 9 months ago Version.php 9 months ago index.php 9 months ago
AbstractQuery.php
645 lines
1 <?php
2 declare (strict_types=1);
3 namespace MailPoetVendor\Doctrine\ORM;
4 if (!defined('ABSPATH')) exit;
5 use BackedEnum;
6 use Countable;
7 use MailPoetVendor\Doctrine\Common\Cache\Psr6\CacheAdapter;
8 use MailPoetVendor\Doctrine\Common\Cache\Psr6\DoctrineProvider;
9 use MailPoetVendor\Doctrine\Common\Collections\ArrayCollection;
10 use MailPoetVendor\Doctrine\Common\Collections\Collection;
11 use MailPoetVendor\Doctrine\DBAL\Cache\QueryCacheProfile;
12 use MailPoetVendor\Doctrine\DBAL\Result;
13 use MailPoetVendor\Doctrine\Deprecations\Deprecation;
14 use MailPoetVendor\Doctrine\ORM\Cache\Exception\InvalidResultCacheDriver;
15 use MailPoetVendor\Doctrine\ORM\Cache\Logging\CacheLogger;
16 use MailPoetVendor\Doctrine\ORM\Cache\QueryCacheKey;
17 use MailPoetVendor\Doctrine\ORM\Cache\TimestampCacheKey;
18 use MailPoetVendor\Doctrine\ORM\Internal\Hydration\IterableResult;
19 use MailPoetVendor\Doctrine\ORM\Mapping\MappingException as ORMMappingException;
20 use MailPoetVendor\Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
21 use MailPoetVendor\Doctrine\ORM\Query\Parameter;
22 use MailPoetVendor\Doctrine\ORM\Query\ResultSetMapping;
23 use MailPoetVendor\Doctrine\Persistence\Mapping\MappingException;
24 use LogicException;
25 use MailPoetVendor\Psr\Cache\CacheItemPoolInterface;
26 use Traversable;
27 use function array_map;
28 use function array_shift;
29 use function assert;
30 use function count;
31 use function func_num_args;
32 use function in_array;
33 use function is_array;
34 use function is_numeric;
35 use function is_object;
36 use function is_scalar;
37 use function is_string;
38 use function iterator_count;
39 use function iterator_to_array;
40 use function ksort;
41 use function method_exists;
42 use function reset;
43 use function serialize;
44 use function sha1;
45 abstract class AbstractQuery
46 {
47 public const HYDRATE_OBJECT = 1;
48 public const HYDRATE_ARRAY = 2;
49 public const HYDRATE_SCALAR = 3;
50 public const HYDRATE_SINGLE_SCALAR = 4;
51 public const HYDRATE_SIMPLEOBJECT = 5;
52 public const HYDRATE_SCALAR_COLUMN = 6;
53 protected $parameters;
54 protected $_resultSetMapping;
55 protected $_em;
56 protected $_hints = [];
57 protected $_hydrationMode = self::HYDRATE_OBJECT;
58 protected $_queryCacheProfile;
59 protected $_expireResultCache = \false;
60 protected $_hydrationCacheProfile;
61 protected $cacheable = \false;
62 protected $hasCache = \false;
63 protected $cacheRegion;
64 protected $cacheMode;
65 protected $cacheLogger;
66 protected $lifetime = 0;
67 public function __construct(EntityManagerInterface $em)
68 {
69 $this->_em = $em;
70 $this->parameters = new ArrayCollection();
71 $this->_hints = $em->getConfiguration()->getDefaultQueryHints();
72 $this->hasCache = $this->_em->getConfiguration()->isSecondLevelCacheEnabled();
73 if ($this->hasCache) {
74 $this->cacheLogger = $em->getConfiguration()->getSecondLevelCacheConfiguration()->getCacheLogger();
75 }
76 }
77 public function setCacheable($cacheable)
78 {
79 $this->cacheable = (bool) $cacheable;
80 return $this;
81 }
82 public function isCacheable()
83 {
84 return $this->cacheable;
85 }
86 public function setCacheRegion($cacheRegion)
87 {
88 $this->cacheRegion = (string) $cacheRegion;
89 return $this;
90 }
91 public function getCacheRegion()
92 {
93 return $this->cacheRegion;
94 }
95 protected function isCacheEnabled()
96 {
97 return $this->cacheable && $this->hasCache;
98 }
99 public function getLifetime()
100 {
101 return $this->lifetime;
102 }
103 public function setLifetime($lifetime)
104 {
105 $this->lifetime = (int) $lifetime;
106 return $this;
107 }
108 public function getCacheMode()
109 {
110 return $this->cacheMode;
111 }
112 public function setCacheMode($cacheMode)
113 {
114 $this->cacheMode = (int) $cacheMode;
115 return $this;
116 }
117 public abstract function getSQL();
118 public function getEntityManager()
119 {
120 return $this->_em;
121 }
122 public function free()
123 {
124 $this->parameters = new ArrayCollection();
125 $this->_hints = $this->_em->getConfiguration()->getDefaultQueryHints();
126 }
127 public function getParameters()
128 {
129 return $this->parameters;
130 }
131 public function getParameter($key)
132 {
133 $key = Query\Parameter::normalizeName($key);
134 $filteredParameters = $this->parameters->filter(static function (Query\Parameter $parameter) use($key) : bool {
135 $parameterName = $parameter->getName();
136 return $key === $parameterName;
137 });
138 return !$filteredParameters->isEmpty() ? $filteredParameters->first() : null;
139 }
140 public function setParameters($parameters)
141 {
142 if (is_array($parameters)) {
143 $parameterCollection = new ArrayCollection();
144 foreach ($parameters as $key => $value) {
145 $parameterCollection->add(new Parameter($key, $value));
146 }
147 $parameters = $parameterCollection;
148 }
149 $this->parameters = $parameters;
150 return $this;
151 }
152 public function setParameter($key, $value, $type = null)
153 {
154 $existingParameter = $this->getParameter($key);
155 if ($existingParameter !== null) {
156 $existingParameter->setValue($value, $type);
157 return $this;
158 }
159 $this->parameters->add(new Parameter($key, $value, $type));
160 return $this;
161 }
162 public function processParameterValue($value)
163 {
164 if (is_scalar($value)) {
165 return $value;
166 }
167 if ($value instanceof Collection) {
168 $value = iterator_to_array($value);
169 }
170 if (is_array($value)) {
171 $value = $this->processArrayParameterValue($value);
172 return $value;
173 }
174 if ($value instanceof Mapping\ClassMetadata) {
175 return $value->name;
176 }
177 if ($value instanceof BackedEnum) {
178 return $value->value;
179 }
180 if (!is_object($value)) {
181 return $value;
182 }
183 try {
184 $class = DefaultProxyClassNameResolver::getClass($value);
185 $value = $this->_em->getUnitOfWork()->getSingleIdentifierValue($value);
186 if ($value === null) {
187 throw ORMInvalidArgumentException::invalidIdentifierBindingEntity($class);
188 }
189 } catch (MappingException|ORMMappingException $e) {
190 $value = $this->potentiallyProcessIterable($value);
191 }
192 return $value;
193 }
194 private function potentiallyProcessIterable($value)
195 {
196 if ($value instanceof Traversable) {
197 $value = iterator_to_array($value);
198 $value = $this->processArrayParameterValue($value);
199 }
200 return $value;
201 }
202 private function processArrayParameterValue(array $value) : array
203 {
204 foreach ($value as $key => $paramValue) {
205 $paramValue = $this->processParameterValue($paramValue);
206 $value[$key] = is_array($paramValue) ? reset($paramValue) : $paramValue;
207 }
208 return $value;
209 }
210 public function setResultSetMapping(Query\ResultSetMapping $rsm)
211 {
212 $this->translateNamespaces($rsm);
213 $this->_resultSetMapping = $rsm;
214 return $this;
215 }
216 protected function getResultSetMapping()
217 {
218 return $this->_resultSetMapping;
219 }
220 private function translateNamespaces(Query\ResultSetMapping $rsm) : void
221 {
222 $translate = function ($alias) : string {
223 return $this->_em->getClassMetadata($alias)->getName();
224 };
225 $rsm->aliasMap = array_map($translate, $rsm->aliasMap);
226 $rsm->declaringClasses = array_map($translate, $rsm->declaringClasses);
227 }
228 public function setHydrationCacheProfile(?QueryCacheProfile $profile = null)
229 {
230 if ($profile === null) {
231 if (func_num_args() < 1) {
232 Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9791', 'Calling %s without arguments is deprecated, pass null instead.', __METHOD__);
233 }
234 $this->_hydrationCacheProfile = null;
235 return $this;
236 }
237 // DBAL 2
238 if (!method_exists(QueryCacheProfile::class, 'setResultCache')) {
239 // @phpstan-ignore method.deprecated
240 if (!$profile->getResultCacheDriver()) {
241 $defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCache();
242 if ($defaultHydrationCacheImpl) {
243 // @phpstan-ignore method.deprecated
244 $profile = $profile->setResultCacheDriver(DoctrineProvider::wrap($defaultHydrationCacheImpl));
245 }
246 }
247 } elseif (!$profile->getResultCache()) {
248 $defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCache();
249 if ($defaultHydrationCacheImpl) {
250 $profile = $profile->setResultCache($defaultHydrationCacheImpl);
251 }
252 }
253 $this->_hydrationCacheProfile = $profile;
254 return $this;
255 }
256 public function getHydrationCacheProfile()
257 {
258 return $this->_hydrationCacheProfile;
259 }
260 public function setResultCacheProfile(?QueryCacheProfile $profile = null)
261 {
262 if ($profile === null) {
263 if (func_num_args() < 1) {
264 Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9791', 'Calling %s without arguments is deprecated, pass null instead.', __METHOD__);
265 }
266 $this->_queryCacheProfile = null;
267 return $this;
268 }
269 // DBAL 2
270 if (!method_exists(QueryCacheProfile::class, 'setResultCache')) {
271 // @phpstan-ignore method.deprecated
272 if (!$profile->getResultCacheDriver()) {
273 $defaultResultCacheDriver = $this->_em->getConfiguration()->getResultCache();
274 if ($defaultResultCacheDriver) {
275 // @phpstan-ignore method.deprecated
276 $profile = $profile->setResultCacheDriver(DoctrineProvider::wrap($defaultResultCacheDriver));
277 }
278 }
279 } elseif (!$profile->getResultCache()) {
280 $defaultResultCache = $this->_em->getConfiguration()->getResultCache();
281 if ($defaultResultCache) {
282 $profile = $profile->setResultCache($defaultResultCache);
283 }
284 }
285 $this->_queryCacheProfile = $profile;
286 return $this;
287 }
288 public function setResultCacheDriver($resultCacheDriver = null)
289 {
290 if ($resultCacheDriver !== null && !$resultCacheDriver instanceof \MailPoetVendor\Doctrine\Common\Cache\Cache) {
291 throw InvalidResultCacheDriver::create();
292 }
293 return $this->setResultCache($resultCacheDriver ? CacheAdapter::wrap($resultCacheDriver) : null);
294 }
295 public function setResultCache(?CacheItemPoolInterface $resultCache = null)
296 {
297 if ($resultCache === null) {
298 if (func_num_args() < 1) {
299 Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9791', 'Calling %s without arguments is deprecated, pass null instead.', __METHOD__);
300 }
301 if ($this->_queryCacheProfile) {
302 $this->_queryCacheProfile = new QueryCacheProfile($this->_queryCacheProfile->getLifetime(), $this->_queryCacheProfile->getCacheKey());
303 }
304 return $this;
305 }
306 // DBAL 2
307 if (!method_exists(QueryCacheProfile::class, 'setResultCache')) {
308 $resultCacheDriver = DoctrineProvider::wrap($resultCache);
309 $this->_queryCacheProfile = $this->_queryCacheProfile ? $this->_queryCacheProfile->setResultCacheDriver($resultCacheDriver) : new QueryCacheProfile(0, null, $resultCacheDriver);
310 return $this;
311 }
312 $this->_queryCacheProfile = $this->_queryCacheProfile ? $this->_queryCacheProfile->setResultCache($resultCache) : new QueryCacheProfile(0, null, $resultCache);
313 return $this;
314 }
315 public function getResultCacheDriver()
316 {
317 if ($this->_queryCacheProfile && $this->_queryCacheProfile->getResultCacheDriver()) {
318 return $this->_queryCacheProfile->getResultCacheDriver();
319 }
320 return $this->_em->getConfiguration()->getResultCacheImpl();
321 }
322 public function useResultCache($useCache, $lifetime = null, $resultCacheId = null)
323 {
324 return $useCache ? $this->enableResultCache($lifetime, $resultCacheId) : $this->disableResultCache();
325 }
326 public function enableResultCache(?int $lifetime = null, ?string $resultCacheId = null) : self
327 {
328 $this->setResultCacheLifetime($lifetime);
329 $this->setResultCacheId($resultCacheId);
330 return $this;
331 }
332 public function disableResultCache() : self
333 {
334 $this->_queryCacheProfile = null;
335 return $this;
336 }
337 public function setResultCacheLifetime($lifetime)
338 {
339 $lifetime = (int) $lifetime;
340 if ($this->_queryCacheProfile) {
341 $this->_queryCacheProfile = $this->_queryCacheProfile->setLifetime($lifetime);
342 return $this;
343 }
344 $this->_queryCacheProfile = new QueryCacheProfile($lifetime);
345 $cache = $this->_em->getConfiguration()->getResultCache();
346 if (!$cache) {
347 return $this;
348 }
349 // Compatibility for DBAL 2
350 if (!method_exists($this->_queryCacheProfile, 'setResultCache')) {
351 // @phpstan-ignore method.deprecated
352 $this->_queryCacheProfile = $this->_queryCacheProfile->setResultCacheDriver(DoctrineProvider::wrap($cache));
353 return $this;
354 }
355 $this->_queryCacheProfile = $this->_queryCacheProfile->setResultCache($cache);
356 return $this;
357 }
358 public function getResultCacheLifetime()
359 {
360 return $this->_queryCacheProfile ? $this->_queryCacheProfile->getLifetime() : 0;
361 }
362 public function expireResultCache($expire = \true)
363 {
364 $this->_expireResultCache = $expire;
365 return $this;
366 }
367 public function getExpireResultCache()
368 {
369 return $this->_expireResultCache;
370 }
371 public function getQueryCacheProfile()
372 {
373 return $this->_queryCacheProfile;
374 }
375 public function setFetchMode($class, $assocName, $fetchMode)
376 {
377 if (!in_array($fetchMode, [Mapping\ClassMetadata::FETCH_EAGER, Mapping\ClassMetadata::FETCH_LAZY], \true)) {
378 Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9777', 'Calling %s() with something else than ClassMetadata::FETCH_EAGER or ClassMetadata::FETCH_LAZY is deprecated.', __METHOD__);
379 $fetchMode = Mapping\ClassMetadata::FETCH_LAZY;
380 }
381 $this->_hints['fetchMode'][$class][$assocName] = $fetchMode;
382 return $this;
383 }
384 public function setHydrationMode($hydrationMode)
385 {
386 $this->_hydrationMode = $hydrationMode;
387 return $this;
388 }
389 public function getHydrationMode()
390 {
391 return $this->_hydrationMode;
392 }
393 public function getResult($hydrationMode = self::HYDRATE_OBJECT)
394 {
395 return $this->execute(null, $hydrationMode);
396 }
397 public function getArrayResult()
398 {
399 return $this->execute(null, self::HYDRATE_ARRAY);
400 }
401 public function getSingleColumnResult()
402 {
403 return $this->execute(null, self::HYDRATE_SCALAR_COLUMN);
404 }
405 public function getScalarResult()
406 {
407 return $this->execute(null, self::HYDRATE_SCALAR);
408 }
409 public function getOneOrNullResult($hydrationMode = null)
410 {
411 try {
412 $result = $this->execute(null, $hydrationMode);
413 } catch (NoResultException $e) {
414 return null;
415 }
416 if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && !$result) {
417 return null;
418 }
419 if (!is_array($result)) {
420 return $result;
421 }
422 if (count($result) > 1) {
423 throw new NonUniqueResultException();
424 }
425 return array_shift($result);
426 }
427 public function getSingleResult($hydrationMode = null)
428 {
429 $result = $this->execute(null, $hydrationMode);
430 if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && !$result) {
431 throw new NoResultException();
432 }
433 if (!is_array($result)) {
434 return $result;
435 }
436 if (count($result) > 1) {
437 throw new NonUniqueResultException();
438 }
439 return array_shift($result);
440 }
441 public function getSingleScalarResult()
442 {
443 return $this->getSingleResult(self::HYDRATE_SINGLE_SCALAR);
444 }
445 public function setHint($name, $value)
446 {
447 $this->_hints[$name] = $value;
448 return $this;
449 }
450 public function getHint($name)
451 {
452 return $this->_hints[$name] ?? \false;
453 }
454 public function hasHint($name)
455 {
456 return isset($this->_hints[$name]);
457 }
458 public function getHints()
459 {
460 return $this->_hints;
461 }
462 public function iterate($parameters = null, $hydrationMode = null)
463 {
464 Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/issues/8463', 'Method %s() is deprecated and will be removed in Doctrine ORM 3.0. Use toIterable() instead.', __METHOD__);
465 if ($hydrationMode !== null) {
466 $this->setHydrationMode($hydrationMode);
467 }
468 if (!empty($parameters)) {
469 $this->setParameters($parameters);
470 }
471 $rsm = $this->getResultSetMapping();
472 if ($rsm === null) {
473 throw new LogicException('Uninitialized result set mapping.');
474 }
475 $stmt = $this->_doExecute();
476 return $this->_em->newHydrator($this->_hydrationMode)->iterate($stmt, $rsm, $this->_hints);
477 }
478 public function toIterable(iterable $parameters = [], $hydrationMode = null) : iterable
479 {
480 if ($hydrationMode !== null) {
481 $this->setHydrationMode($hydrationMode);
482 }
483 if ($this->isCountable($parameters) && count($parameters) !== 0 || $parameters instanceof Traversable && iterator_count($parameters) !== 0) {
484 $this->setParameters($parameters);
485 }
486 $rsm = $this->getResultSetMapping();
487 if ($rsm === null) {
488 throw new LogicException('Uninitialized result set mapping.');
489 }
490 $stmt = $this->_doExecute();
491 return $this->_em->newHydrator($this->_hydrationMode)->toIterable($stmt, $rsm, $this->_hints);
492 }
493 public function execute($parameters = null, $hydrationMode = null)
494 {
495 if ($this->cacheable && $this->isCacheEnabled()) {
496 return $this->executeUsingQueryCache($parameters, $hydrationMode);
497 }
498 return $this->executeIgnoreQueryCache($parameters, $hydrationMode);
499 }
500 private function executeIgnoreQueryCache($parameters = null, $hydrationMode = null)
501 {
502 if ($hydrationMode !== null) {
503 $this->setHydrationMode($hydrationMode);
504 }
505 if (!empty($parameters)) {
506 $this->setParameters($parameters);
507 }
508 $setCacheEntry = static function ($data) : void {
509 };
510 if ($this->_hydrationCacheProfile !== null) {
511 [$cacheKey, $realCacheKey] = $this->getHydrationCacheId();
512 $cache = $this->getHydrationCache();
513 $cacheItem = $cache->getItem($cacheKey);
514 $result = $cacheItem->isHit() ? $cacheItem->get() : [];
515 if (isset($result[$realCacheKey])) {
516 return $result[$realCacheKey];
517 }
518 if (!$result) {
519 $result = [];
520 }
521 $setCacheEntry = static function ($data) use($cache, $result, $cacheItem, $realCacheKey) : void {
522 $cache->save($cacheItem->set($result + [$realCacheKey => $data]));
523 };
524 }
525 $stmt = $this->_doExecute();
526 if (is_numeric($stmt)) {
527 $setCacheEntry($stmt);
528 return $stmt;
529 }
530 $rsm = $this->getResultSetMapping();
531 if ($rsm === null) {
532 throw new LogicException('Uninitialized result set mapping.');
533 }
534 $data = $this->_em->newHydrator($this->_hydrationMode)->hydrateAll($stmt, $rsm, $this->_hints);
535 $setCacheEntry($data);
536 return $data;
537 }
538 private function getHydrationCache() : CacheItemPoolInterface
539 {
540 assert($this->_hydrationCacheProfile !== null);
541 // Support for DBAL 2
542 if (!method_exists($this->_hydrationCacheProfile, 'getResultCache')) {
543 // @phpstan-ignore method.deprecated
544 $cacheDriver = $this->_hydrationCacheProfile->getResultCacheDriver();
545 assert($cacheDriver !== null);
546 return CacheAdapter::wrap($cacheDriver);
547 }
548 $cache = $this->_hydrationCacheProfile->getResultCache();
549 assert($cache !== null);
550 return $cache;
551 }
552 private function executeUsingQueryCache($parameters = null, $hydrationMode = null)
553 {
554 $rsm = $this->getResultSetMapping();
555 if ($rsm === null) {
556 throw new LogicException('Uninitialized result set mapping.');
557 }
558 $queryCache = $this->_em->getCache()->getQueryCache($this->cacheRegion);
559 $queryKey = new QueryCacheKey($this->getHash(), $this->lifetime, $this->cacheMode ?: Cache::MODE_NORMAL, $this->getTimestampKey());
560 $result = $queryCache->get($queryKey, $rsm, $this->_hints);
561 if ($result !== null) {
562 if ($this->cacheLogger) {
563 $this->cacheLogger->queryCacheHit($queryCache->getRegion()->getName(), $queryKey);
564 }
565 return $result;
566 }
567 $result = $this->executeIgnoreQueryCache($parameters, $hydrationMode);
568 $cached = $queryCache->put($queryKey, $rsm, $result, $this->_hints);
569 if ($this->cacheLogger) {
570 $this->cacheLogger->queryCacheMiss($queryCache->getRegion()->getName(), $queryKey);
571 if ($cached) {
572 $this->cacheLogger->queryCachePut($queryCache->getRegion()->getName(), $queryKey);
573 }
574 }
575 return $result;
576 }
577 private function getTimestampKey() : ?TimestampCacheKey
578 {
579 assert($this->_resultSetMapping !== null);
580 $entityName = reset($this->_resultSetMapping->aliasMap);
581 if (empty($entityName)) {
582 return null;
583 }
584 $metadata = $this->_em->getClassMetadata($entityName);
585 return new Cache\TimestampCacheKey($metadata->rootEntityName);
586 }
587 protected function getHydrationCacheId()
588 {
589 $parameters = [];
590 $types = [];
591 foreach ($this->getParameters() as $parameter) {
592 $parameters[$parameter->getName()] = $this->processParameterValue($parameter->getValue());
593 $types[$parameter->getName()] = $parameter->getType();
594 }
595 $sql = $this->getSQL();
596 assert(is_string($sql));
597 $queryCacheProfile = $this->getHydrationCacheProfile();
598 $hints = $this->getHints();
599 $hints['hydrationMode'] = $this->getHydrationMode();
600 ksort($hints);
601 assert($queryCacheProfile !== null);
602 return $queryCacheProfile->generateCacheKeys($sql, $parameters, $types, $hints);
603 }
604 public function setResultCacheId($id)
605 {
606 if (!$this->_queryCacheProfile) {
607 return $this->setResultCacheProfile(new QueryCacheProfile(0, $id));
608 }
609 $this->_queryCacheProfile = $this->_queryCacheProfile->setCacheKey($id);
610 return $this;
611 }
612 public function getResultCacheId()
613 {
614 return $this->_queryCacheProfile ? $this->_queryCacheProfile->getCacheKey() : null;
615 }
616 protected abstract function _doExecute();
617 public function __clone()
618 {
619 $this->parameters = new ArrayCollection();
620 $this->_hints = [];
621 $this->_hints = $this->_em->getConfiguration()->getDefaultQueryHints();
622 }
623 protected function getHash()
624 {
625 $query = $this->getSQL();
626 assert(is_string($query));
627 $hints = $this->getHints();
628 $params = array_map(function (Parameter $parameter) {
629 $value = $parameter->getValue();
630 // Small optimization
631 // Does not invoke processParameterValue for scalar value
632 if (is_scalar($value)) {
633 return $value;
634 }
635 return $this->processParameterValue($value);
636 }, $this->parameters->getValues());
637 ksort($hints);
638 return sha1($query . '-' . serialize($params) . '-' . serialize($hints));
639 }
640 private function isCountable(iterable $subject) : bool
641 {
642 return $subject instanceof Countable || is_array($subject);
643 }
644 }
645