TYPO3 CMS  TYPO3_6-2
QueryResult.php
Go to the documentation of this file.
1 <?php
3 
17 
24 
29  protected $dataMapper;
30 
36 
40  protected $numberOfResults;
41 
45  protected $query;
46 
51  protected $queryResult;
52 
58  public function __construct(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
59  $this->query = $query;
60  }
61 
67  protected function initialize() {
68  if (!is_array($this->queryResult)) {
69  $this->queryResult = $this->dataMapper->map($this->query->getType(), $this->persistenceManager->getObjectDataByQuery($this->query));
70  }
71  }
72 
79  public function getQuery() {
80  return clone $this->query;
81  }
82 
89  public function getFirst() {
90  if (is_array($this->queryResult)) {
92  reset($queryResult);
93  } else {
94  $query = $this->getQuery();
95  $query->setLimit(1);
96  $queryResult = $this->dataMapper->map($query->getType(), $this->persistenceManager->getObjectDataByQuery($query));
97  }
98  $firstResult = current($queryResult);
99  if ($firstResult === FALSE) {
100  $firstResult = NULL;
101  }
102  return $firstResult;
103  }
104 
111  public function count() {
112  if ($this->numberOfResults === NULL) {
113  if (is_array($this->queryResult)) {
114  $this->numberOfResults = count($this->queryResult);
115  } else {
116  $this->numberOfResults = $this->persistenceManager->getObjectCountByQuery($this->query);
117  }
118  }
119  return $this->numberOfResults;
120  }
121 
128  public function toArray() {
129  $this->initialize();
130  return iterator_to_array($this);
131  }
132 
141  public function offsetExists($offset) {
142  $this->initialize();
143  return isset($this->queryResult[$offset]);
144  }
145 
151  public function offsetGet($offset) {
152  $this->initialize();
153  return isset($this->queryResult[$offset]) ? $this->queryResult[$offset] : NULL;
154  }
155 
164  public function offsetSet($offset, $value) {
165  $this->initialize();
166  $this->queryResult[$offset] = $value;
167  }
168 
176  public function offsetUnset($offset) {
177  $this->initialize();
178  unset($this->queryResult[$offset]);
179  }
180 
185  public function current() {
186  $this->initialize();
187  return current($this->queryResult);
188  }
189 
194  public function key() {
195  $this->initialize();
196  return key($this->queryResult);
197  }
198 
203  public function next() {
204  $this->initialize();
205  next($this->queryResult);
206  }
207 
212  public function rewind() {
213  $this->initialize();
214  reset($this->queryResult);
215  }
216 
221  public function valid() {
222  $this->initialize();
223  return current($this->queryResult) !== FALSE;
224  }
225 
229  public function __wakeup() {
230  $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
231  $this->persistenceManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
232  $this->dataMapper = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
233  }
234 
238  public function __sleep() {
239  return array('query');
240  }
241 }
__construct(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
Definition: QueryResult.php:58