‪TYPO3CMS  10.4
QueryResult.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
25 
30 {
34  protected ‪$dataMapper;
35 
39  protected ‪$persistenceManager;
40 
44  protected ‪$numberOfResults;
45 
49  protected ‪$query;
50 
54  protected ‪$queryResult;
55 
59  protected ‪$objectManager;
60 
65  {
66  $this->objectManager = ‪$objectManager;
67  }
68 
73  {
74  $this->persistenceManager = ‪$persistenceManager;
75  }
76 
83  {
84  $this->query = ‪$query;
85  }
86 
90  public function ‪initializeObject()
91  {
92  $this->dataMapper = $this->objectManager->get(DataMapper::class);
93  $this->dataMapper->setQuery($this->query);
94  }
95 
99  protected function ‪initialize()
100  {
101  if (!is_array($this->queryResult)) {
102  $this->queryResult = $this->dataMapper->map($this->query->getType(), $this->persistenceManager->getObjectDataByQuery($this->query));
103  }
104  }
105 
111  public function ‪getQuery()
112  {
113  return clone ‪$this->query;
114  }
115 
121  public function ‪getFirst()
122  {
123  if (is_array($this->queryResult)) {
125  reset(‪$queryResult);
126  } else {
127  ‪$query = $this->‪getQuery();
129  ‪$queryResult = $this->dataMapper->map(‪$query->‪getType(), $this->persistenceManager->getObjectDataByQuery(‪$query));
130  }
131  $firstResult = ‪current(‪$queryResult);
132  if ($firstResult === false) {
133  $firstResult = null;
134  }
135  return $firstResult;
136  }
137 
143  public function ‪count()
144  {
145  if ($this->numberOfResults === null) {
146  if (is_array($this->queryResult)) {
147  $this->numberOfResults = ‪count($this->queryResult);
148  } else {
149  $this->numberOfResults = $this->persistenceManager->getObjectCountByQuery($this->query);
150  }
151  }
153  }
154 
160  public function ‪toArray()
161  {
162  $this->‪initialize();
163  return iterator_to_array($this);
164  }
165 
174  public function ‪offsetExists($offset)
175  {
176  $this->‪initialize();
177  return isset($this->queryResult[$offset]);
178  }
179 
185  public function ‪offsetGet($offset)
186  {
187  $this->‪initialize();
188  return $this->queryResult[$offset] ?? null;
189  }
190 
198  public function ‪offsetSet($offset, $value)
199  {
200  $this->‪initialize();
201  $this->numberOfResults = null;
202  $this->queryResult[$offset] = $value;
203  }
204 
211  public function ‪offsetUnset($offset)
212  {
213  $this->‪initialize();
214  $this->numberOfResults = null;
215  unset($this->queryResult[$offset]);
216  }
217 
222  public function ‪current()
223  {
224  $this->‪initialize();
225  return ‪current($this->queryResult);
226  }
227 
232  public function ‪key()
233  {
234  $this->‪initialize();
235  return ‪key($this->queryResult);
236  }
237 
241  public function ‪next()
242  {
243  $this->‪initialize();
244  ‪next($this->queryResult);
245  }
246 
250  public function ‪rewind()
251  {
252  $this->‪initialize();
253  reset($this->queryResult);
254  }
255 
260  public function ‪valid()
261  {
262  $this->‪initialize();
263  return ‪current($this->queryResult) !== false;
264  }
265 
271  public function ‪__wakeup()
272  {
273  ‪$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
274  $this->persistenceManager = ‪$objectManager->‪get(PersistenceManagerInterface::class);
275  $this->dataMapper = ‪$objectManager->‪get(DataMapper::class);
276  }
277 
282  public function ‪__sleep()
283  {
284  return ['query'];
285  }
286 }
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$dataMapper
‪TYPO3 CMS Extbase Persistence Generic Mapper DataMapper $dataMapper
Definition: QueryResult.php:33
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\offsetGet
‪mixed offsetGet($offset)
Definition: QueryResult.php:179
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:29
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$objectManager
‪ObjectManagerInterface $objectManager
Definition: QueryResult.php:53
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\getFirst
‪object getFirst()
Definition: QueryResult.php:115
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\offsetSet
‪offsetSet($offset, $value)
Definition: QueryResult.php:192
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:52
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\offsetExists
‪bool offsetExists($offset)
Definition: QueryResult.php:168
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\count
‪int count()
Definition: QueryResult.php:137
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\__construct
‪__construct(QueryInterface $query)
Definition: QueryResult.php:76
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\key
‪mixed key()
Definition: QueryResult.php:226
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\__wakeup
‪__wakeup()
Definition: QueryResult.php:265
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$queryResult
‪array $queryResult
Definition: QueryResult.php:49
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\next
‪next()
Definition: QueryResult.php:235
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\rewind
‪rewind()
Definition: QueryResult.php:244
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\valid
‪bool valid()
Definition: QueryResult.php:254
‪TYPO3\CMS\Extbase\Persistence\Generic
Definition: Backend.php:16
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$numberOfResults
‪int null $numberOfResults
Definition: QueryResult.php:41
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\injectPersistenceManager
‪injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
Definition: QueryResult.php:66
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\setLimit
‪TYPO3 CMS Extbase Persistence QueryInterface setLimit($limit)
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
Definition: QueryResult.php:30
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\getType
‪string getType()
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\injectObjectManager
‪injectObjectManager(ObjectManagerInterface $objectManager)
Definition: QueryResult.php:58
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\current
‪mixed current()
Definition: QueryResult.php:216
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\initialize
‪initialize()
Definition: QueryResult.php:93
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\initializeObject
‪initializeObject()
Definition: QueryResult.php:84
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\offsetUnset
‪offsetUnset($offset)
Definition: QueryResult.php:205
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$query
‪TYPO3 CMS Extbase Persistence QueryInterface $query
Definition: QueryResult.php:45
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface\get
‪object &T get(string $className,... $constructorArguments)
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$persistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $persistenceManager
Definition: QueryResult.php:37
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\toArray
‪array toArray()
Definition: QueryResult.php:154
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\__sleep
‪array __sleep()
Definition: QueryResult.php:276
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\getQuery
‪TYPO3 CMS Extbase Persistence QueryInterface getQuery()
Definition: QueryResult.php:105