‪TYPO3CMS  9.5
QueryResult.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
25 {
29  protected ‪$dataMapper;
30 
34  protected ‪$persistenceManager;
35 
39  protected ‪$numberOfResults;
40 
44  protected ‪$query;
45 
49  protected ‪$queryResult;
50 
54  protected ‪$objectManager;
55 
60  {
61  $this->objectManager = ‪$objectManager;
62  }
63 
68  {
69  $this->persistenceManager = ‪$persistenceManager;
70  }
71 
77  public function ‪__construct(\‪TYPO3\CMS\‪Extbase\Persistence\‪QueryInterface ‪$query)
78  {
79  $this->query = ‪$query;
80  }
81 
85  public function ‪initializeObject()
86  {
87  $this->dataMapper = $this->objectManager->get(DataMapper::class, $this->query);
88  }
89 
93  protected function ‪initialize()
94  {
95  if (!is_array($this->queryResult)) {
96  $this->queryResult = $this->dataMapper->map($this->query->getType(), $this->persistenceManager->getObjectDataByQuery($this->query));
97  }
98  }
99 
105  public function ‪getQuery()
106  {
107  return clone ‪$this->query;
108  }
109 
115  public function ‪getFirst()
116  {
117  if (is_array($this->queryResult)) {
119  reset(‪$queryResult);
120  } else {
121  ‪$query = $this->‪getQuery();
123  ‪$queryResult = $this->dataMapper->map(‪$query->‪getType(), $this->persistenceManager->getObjectDataByQuery(‪$query));
124  }
125  $firstResult = ‪current(‪$queryResult);
126  if ($firstResult === false) {
127  $firstResult = null;
128  }
129  return $firstResult;
130  }
131 
137  public function ‪count()
138  {
139  if ($this->numberOfResults === null) {
140  if (is_array($this->queryResult)) {
141  $this->numberOfResults = ‪count($this->queryResult);
142  } else {
143  $this->numberOfResults = $this->persistenceManager->getObjectCountByQuery($this->query);
144  }
145  }
147  }
148 
154  public function ‪toArray()
155  {
156  $this->‪initialize();
157  return iterator_to_array($this);
158  }
159 
168  public function ‪offsetExists($offset)
169  {
170  $this->‪initialize();
171  return isset($this->queryResult[$offset]);
172  }
173 
179  public function ‪offsetGet($offset)
180  {
181  $this->‪initialize();
182  return $this->queryResult[$offset] ?? null;
183  }
184 
192  public function ‪offsetSet($offset, $value)
193  {
194  $this->‪initialize();
195  $this->numberOfResults = null;
196  $this->queryResult[$offset] = $value;
197  }
198 
205  public function ‪offsetUnset($offset)
206  {
207  $this->‪initialize();
208  $this->numberOfResults = null;
209  unset($this->queryResult[$offset]);
210  }
211 
216  public function ‪current()
217  {
218  $this->‪initialize();
219  return ‪current($this->queryResult);
220  }
221 
226  public function ‪key()
227  {
228  $this->‪initialize();
229  return ‪key($this->queryResult);
230  }
231 
235  public function ‪next()
236  {
237  $this->‪initialize();
238  ‪next($this->queryResult);
239  }
240 
244  public function ‪rewind()
245  {
246  $this->‪initialize();
247  reset($this->queryResult);
248  }
249 
254  public function ‪valid()
255  {
256  $this->‪initialize();
257  return ‪current($this->queryResult) !== false;
258  }
259 
265  public function ‪__wakeup()
266  {
267  ‪$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\‪TYPO3\CMS\‪Extbase\Object\ObjectManager::class);
268  $this->persistenceManager = ‪$objectManager->‪get(\‪TYPO3\CMS\‪Extbase\Persistence\PersistenceManagerInterface::class);
269  $this->dataMapper = ‪$objectManager->‪get(DataMapper::class);
270  }
271 
276  public function ‪__sleep()
277  {
278  return ['query'];
279  }
280 }
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$dataMapper
‪TYPO3 CMS Extbase Persistence Generic Mapper DataMapper $dataMapper
Definition: QueryResult.php:28
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface\get
‪object &T get($objectName,... $constructorArguments)
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:21
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\offsetGet
‪mixed offsetGet($offset)
Definition: QueryResult.php:173
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:26
‪TYPO3
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\__construct
‪__construct(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
Definition: QueryResult.php:71
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$objectManager
‪ObjectManagerInterface $objectManager
Definition: QueryResult.php:48
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\getFirst
‪object getFirst()
Definition: QueryResult.php:109
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\offsetSet
‪offsetSet($offset, $value)
Definition: QueryResult.php:186
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:32
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\offsetExists
‪bool offsetExists($offset)
Definition: QueryResult.php:162
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\injectPersistenceManager
‪injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
Definition: QueryResult.php:61
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\count
‪int count()
Definition: QueryResult.php:131
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:23
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\key
‪mixed key()
Definition: QueryResult.php:220
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\__wakeup
‪__wakeup()
Definition: QueryResult.php:259
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$queryResult
‪array $queryResult
Definition: QueryResult.php:44
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\next
‪next()
Definition: QueryResult.php:229
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\rewind
‪rewind()
Definition: QueryResult.php:238
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:21
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\valid
‪bool valid()
Definition: QueryResult.php:248
‪TYPO3\CMS\Extbase\Persistence\Generic
Definition: Backend.php:2
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$numberOfResults
‪int null $numberOfResults
Definition: QueryResult.php:36
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\setLimit
‪TYPO3 CMS Extbase Persistence QueryInterface setLimit($limit)
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
Definition: QueryResult.php:25
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\getType
‪string getType()
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\injectObjectManager
‪injectObjectManager(ObjectManagerInterface $objectManager)
Definition: QueryResult.php:53
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\current
‪mixed current()
Definition: QueryResult.php:210
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\initialize
‪initialize()
Definition: QueryResult.php:87
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\initializeObject
‪initializeObject()
Definition: QueryResult.php:79
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\offsetUnset
‪offsetUnset($offset)
Definition: QueryResult.php:199
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$query
‪TYPO3 CMS Extbase Persistence QueryInterface $query
Definition: QueryResult.php:40
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\$persistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $persistenceManager
Definition: QueryResult.php:32
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\toArray
‪array toArray()
Definition: QueryResult.php:148
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\__sleep
‪array __sleep()
Definition: QueryResult.php:270
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\getQuery
‪TYPO3 CMS Extbase Persistence QueryInterface getQuery()
Definition: QueryResult.php:99