TYPO3 API  SVNRelease
QueryResult.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2010 Bastian Waidelich <bastian@typo3.org>
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * A lazy result list that is returned by Query::execute()
00030  *
00031  * @package Extbase
00032  * @subpackage Persistence
00033  * @scope prototype
00034  * @api
00035  */
00036 class Tx_Extbase_Persistence_QueryResult implements Tx_Extbase_Persistence_QueryResultInterface {
00037 
00038     /**
00039      * This field is only needed to make debugging easier:
00040      * If you call current() on a class that implements Iterator, PHP will return the first field of the object
00041      * instead of calling the current() method of the interface.
00042      * We use this unusual behavior of PHP to return the warning below in this case.
00043      *
00044      * @var string
00045      * @deprecated since Extbase 1.3.0; will be removed in Extbase 1.5.0
00046      */
00047     private $warning = 'You should never see this warning. If you do, you probably used PHP array functions like current() on the Tx_Extbase_Persistence_QueryResult. To retrieve the first result, you can use the getFirst() method.';
00048 
00049     /**
00050      * @var Tx_Extbase_Persistence_Mapper_DataMapper
00051      */
00052     protected $dataMapper;
00053 
00054     /**
00055      * @var Tx_Extbase_Persistence_ManagerInterface
00056      */
00057     protected $persistenceManager;
00058 
00059     /**
00060      * @var Tx_Extbase_Persistence_QueryInterface
00061      */
00062     protected $query;
00063 
00064     /**
00065      * @var array
00066      * @transient
00067      */
00068     protected $queryResult;
00069 
00070     /**
00071      * Constructor
00072      *
00073      * @param Tx_Extbase_Persistence_QueryInterface $query
00074      */
00075     public function __construct(Tx_Extbase_Persistence_QueryInterface $query) {
00076         $this->query = $query;
00077     }
00078 
00079     /**
00080      * Injects the DataMapper to map records to objects
00081      *
00082      * @param Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper
00083      * @return void
00084      */
00085     public function injectDataMapper(Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper) {
00086         $this->dataMapper = $dataMapper;
00087     }
00088 
00089     /**
00090      * Injects the persistence manager
00091      *
00092      * @param Tx_Extbase_Persistence_ManagerInterface $persistenceManager
00093      * @return void
00094      */
00095     public function injectPersistenceManager(Tx_Extbase_Persistence_ManagerInterface $persistenceManager) {
00096         $this->persistenceManager = $persistenceManager;
00097     }
00098 
00099     /**
00100      * Loads the objects this QueryResult is supposed to hold
00101      *
00102      * @return void
00103      */
00104     protected function initialize() {
00105         if (!is_array($this->queryResult)) {
00106             $this->queryResult = $this->dataMapper->map($this->query->getType(), $this->persistenceManager->getObjectDataByQuery($this->query));
00107         }
00108     }
00109 
00110     /**
00111      * Returns a clone of the query object
00112      *
00113      * @return Tx_Extbase_Persistence_QueryInterface
00114      * @api
00115      */
00116     public function getQuery() {
00117         return clone $this->query;
00118     }
00119 
00120     /**
00121      * Returns the first object in the result set
00122      *
00123      * @return object
00124      * @api
00125      */
00126     public function getFirst() {
00127         if (is_array($this->queryResult)) {
00128             $queryResult = $this->queryResult;
00129             reset($queryResult);
00130         } else {
00131             $query = $this->getQuery();
00132             $query->setLimit(1);
00133             $queryResult = $this->dataMapper->map($query->getType(), $this->persistenceManager->getObjectDataByQuery($query));
00134         }
00135         $firstResult = current($queryResult);
00136         if ($firstResult === FALSE) {
00137             $firstResult = NULL;
00138         }
00139         return $firstResult;
00140     }
00141 
00142     /**
00143      * Returns the number of objects in the result
00144      *
00145      * @return integer The number of matching objects
00146      * @api
00147      */
00148     public function count() {
00149         if (is_array($this->queryResult)) {
00150             return count($this->queryResult);
00151         } else {
00152             return $this->persistenceManager->getObjectCountByQuery($this->query);
00153         }
00154     }
00155 
00156     /**
00157      * Returns an array with the objects in the result set
00158      *
00159      * @return array
00160      * @api
00161      */
00162     public function toArray() {
00163         $this->initialize();
00164         return iterator_to_array($this);
00165     }
00166 
00167     /**
00168      * This method is needed to implement the ArrayAccess interface,
00169      * but it isn't very useful as the offset has to be an integer
00170      *
00171      * @param mixed $offset
00172      * @return boolean
00173      * @see ArrayAccess::offsetExists()
00174      */
00175     public function offsetExists($offset) {
00176         $this->initialize();
00177         return isset($this->queryResult[$offset]);
00178     }
00179 
00180     /**
00181      * @param mixed $offset
00182      * @return mixed
00183      * @see ArrayAccess::offsetGet()
00184      */
00185     public function offsetGet($offset) {
00186         $this->initialize();
00187         return isset($this->queryResult[$offset]) ? $this->queryResult[$offset] : NULL;
00188     }
00189 
00190     /**
00191      * This method has no effect on the persisted objects but only on the result set
00192      *
00193      * @param mixed $offset
00194      * @param mixed $value
00195      * @return void
00196      * @see ArrayAccess::offsetSet()
00197      */
00198     public function offsetSet($offset, $value) {
00199         $this->initialize();
00200         $this->queryResult[$offset] = $value;
00201     }
00202 
00203     /**
00204      * This method has no effect on the persisted objects but only on the result set
00205      *
00206      * @param mixed $offset
00207      * @return void
00208      * @see ArrayAccess::offsetUnset()
00209      */
00210     public function offsetUnset($offset) {
00211         $this->initialize();
00212         unset($this->queryResult[$offset]);
00213     }
00214 
00215     /**
00216      * @return mixed
00217      * @see Iterator::current()
00218      */
00219     public function current() {
00220         $this->initialize();
00221         return current($this->queryResult);
00222     }
00223 
00224     /**
00225      * @return mixed
00226      * @see Iterator::key()
00227      */
00228     public function key() {
00229         $this->initialize();
00230         return key($this->queryResult);
00231     }
00232 
00233     /**
00234      * @return void
00235      * @see Iterator::next()
00236      */
00237     public function next() {
00238         $this->initialize();
00239         next($this->queryResult);
00240     }
00241 
00242     /**
00243      * @return void
00244      * @see Iterator::rewind()
00245      */
00246     public function rewind() {
00247         $this->initialize();
00248         reset($this->queryResult);
00249     }
00250 
00251     /**
00252      * @return void
00253      * @see Iterator::valid()
00254      */
00255     public function valid() {
00256         $this->initialize();
00257         return current($this->queryResult) !== FALSE;
00258     }
00259 
00260     /**
00261      * @return void
00262      */
00263     public function __wakeup() {
00264         $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
00265         $this->persistenceManager = $objectManager->get('Tx_Extbase_Persistence_ManagerInterface');
00266         $this->dataMapper = $objectManager->get('Tx_Extbase_Persistence_Mapper_DataMapper');
00267     }
00268 
00269     /**
00270      * @return array
00271      */
00272     public function __sleep() {
00273         return array('query');
00274     }
00275 }
00276 ?>