Guest User

Franz

a guest
Feb 17th, 2010
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1.     /**
  2.      * Processes a SQL query and returns proper objects
  3.      * @param resource $mySqlResourceId The Pointer to the mySql resource of the query
  4.      * @param string $objectType The object type the result rows shall get mapped on. Is falling back to objectType of the repository if not defined otherwhise.
  5.      * @param string $returnAs The type the result shall be returned as. Valid are "array" and "Tx_Extbase_Persistence_ObjectStorage"
  6.      * @return array/Tx_Extbase_Persistence_ObjectStorage depending on configuration
  7.      */
  8.     public function mapQueryResultsToObjects($mySqlResourceId, $offset=NULL, $limit=NULL, $objectType = NULL, $returnAs = 'array') {
  9.         $returnAs = strtolower($returnAs);
  10.         $objectType = $objectType ? $objectType : $this->objectType;
  11.  
  12.         $result = NULL;
  13.  
  14.         if($mySqlResourceId) {
  15.             $result = ($returnAs == 'array' ? array() : t3lib_div::makeInstance('Tx_Extbase_Persistence_ObjectStorage'));
  16.             $rowIterator = t3lib_div::makeInstance('Tx_Extbase_Persistence_RowIterator');
  17.  
  18.             if ($this->getEnableResultCount()) {
  19.                 $count = $GLOBALS['TYPO3_DB']->sql_num_rows($mySqlResourceId);
  20.                 $this->setLastResultCount($count);
  21.  
  22.                 if ($count) {
  23.    
  24.                     if ($offset && $offset > 0) {
  25.                         $GLOBALS['TYPO3_DB']->sql_data_seek($mySqlResourceId,intval($offset));
  26.                     }
  27.                    
  28.                     $iterationLimit = min($limit,$count);
  29.                     $cycle = 0;
  30.                    
  31.                     # fetch query results and map them to persistence rows expected by the dataMapper
  32.                     while ($cycle < $iterationLimit && $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mySqlResourceId)) {
  33.                         $cycle++;
  34.                         $rowIterator->append(t3lib_div::makeInstance('Tx_Extbase_Persistence_Row',$row));
  35.                     }
  36.                 }
  37.             } else {
  38.                 # fetch query results and map them to persistence rows expected by the dataMapper
  39.                 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mySqlResourceId)) {
  40.                     $rowIterator->append(t3lib_div::makeInstance('Tx_Extbase_Persistence_Row',$row));
  41.                 }
  42.             }
  43.             $GLOBALS['TYPO3_DB']->sql_free_result($mySqlResourceId);
  44.  
  45.             # map the query results to objects
  46.             $dataMapper = Tx_Extbase_Dispatcher::getPersistenceManager()->getBackend()->getDataMapper();
  47.             $objects = $dataMapper->map($objectType,$rowIterator);
  48.  
  49.             # prepare objects for the correct return type
  50.             if(is_object($result)) {
  51.                 $result->addAll($objects);
  52.                 $result->_memorizeCleanState();
  53.             } else {
  54.                 $result = $objects;
  55.             }
  56.         }
  57.         return $result;
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment