Example #1
0
 /**
  * Check whether internal resource has rows to fetch
  *
  * @return boolean
  */
 public function valid()
 {
     if ($this->_type === 1) {
         //The result is bigger than 32 rows so it's retrieved one by one
         if ($this->_result !== false) {
             $row = $this->_result->fetch($this->_result);
         } else {
             $row = false;
         }
     } else {
         //The full rows are dumped into $this->rows
         if (is_array($this->_rows) === true) {
             $row = current($this->_rows);
             if (is_object($row) === true) {
                 next($this->_rows);
             }
         } else {
             $row = false;
         }
     }
     //Valid records are arrays
     if (is_array($row) === true || is_object($row) === true) {
         //The result type=1 so we need to build every row
         if ($this->_type === 1) {
             //Each row in a complex result is a Phalcon\Mvc\Model\Row instance
             switch ((int) $this->_hydrateMode) {
                 case 0:
                     $activeRow = new Row();
                     break;
                 case 1:
                     $activeRow = array();
                     break;
                 case 2:
                     $activeRow = new stdClass();
                     break;
                     //@note no default exception
             }
             //Set records as dirty state PERSISTENT by default
             $dirtyState = 0;
             foreach ($this->_columnTypes as $alias => $column) {
                 if ($column['type'] === 'object') {
                     //Object columns are assigned column by column
                     $columnMap = $column['columnMap'];
                     $attributes = $column['attributes'];
                     $rowModel = array();
                     foreach ($column['attributes'] as $attribute) {
                         //Columns are supposed to be in the form _table_field
                         $rowModel[$attribute] = $row['_' . $column['column'] . '_' . $attribute];
                     }
                     //Generate the column value according to the hydration type
                     switch ((int) $this->_hydrateMode) {
                         case 0:
                             //Check if the resultset must keep snapshots
                             if (isset($column['keepSnapshots']) === true) {
                                 $keepSnapshots = $column['keepSnapshots'];
                             } else {
                                 $keepSnapshots = false;
                             }
                             //Get the base instance
                             $instance = $column['instance'];
                             //Assign the values to the attributes using a column map
                             $value = Model::cloneResultMap($instance, $rowModel, $columnMap, $dirtyState, $keepSnapshots);
                             break;
                         default:
                             //Other kinds of hydrations
                             $value = Model::cloneResultMapHydrate($rowModel, $columnMap, $this->_hydrateMode);
                             break;
                     }
                     //The complete object is assigned to an attribute with the name of the alias or
                     //the model name
                     $attribute = null;
                     if (isset($column['balias']) === true) {
                         $attribute = $column['balias'];
                     }
                 } else {
                     //Scalar columns are simply assigned to the result objects
                     if (isset($column['sqlAlias']) === true) {
                         $value = $row[$column['sqlAlias']];
                     } else {
                         if (isset($row[$alias]) === true) {
                             $value = $row[$alias];
                         }
                     }
                     //If a 'balias' is defined it is not an unnamed scalar
                     if (isset($column['balias']) === true) {
                         $attribute = $alias;
                     } else {
                         $attribute = str_replace('_', '', $alias);
                     }
                 }
                 if (isset($attribute) === false) {
                     $attribute = null;
                 }
                 //Assign the instance according to the hydration type
                 switch ((int) $this->_hydrateMode) {
                     case 1:
                         $activeRow[$attribute] = $value;
                         break;
                     default:
                         $activeRow->{$attribute} = $value;
                         break;
                 }
             }
             //Store the generated row in $this->activeRow to be retrieved by 'current'
             $this->_activeRow = $activeRow;
         } else {
             //The row is already build so we just assign it to the activeRow
             $this->_activeRow = $row;
         }
         return true;
     }
     //There are no results to retrieve so we update $this->activeRow as false
     $this->_activeRow = false;
     return false;
 }