Ejemplo n.º 1
0
 /**
  * Loads a database result, either as a new record for this model, or as
  * an iterator for multiple rows.
  *
  * @chainable
  * @param  bool $multiple Return an iterator or load a single row
  * @return ORM|Database_Result
  */
 protected function _load_result($multiple = FALSE)
 {
     $this->_db_builder->from(array($this->_table_name, $this->_object_name));
     if ($multiple === FALSE) {
         // Only fetch 1 record
         $this->_db_builder->limit(1);
     }
     // Select all columns by default
     $this->_db_builder->select_array($this->_build_select());
     if (!isset($this->_db_applied['order_by']) and !empty($this->_sorting)) {
         foreach ($this->_sorting as $column => $direction) {
             if (strpos($column, '.') === FALSE) {
                 // Sorting column for use in JOINs
                 $column = $this->_object_name . '.' . $column;
             }
             $this->_db_builder->order_by($column, $direction);
         }
     }
     if ($multiple === TRUE) {
         // Return database iterator casting to this object type
         $result = $this->_db_builder->as_object(get_class($this))->execute($this->_db);
         $this->reset();
         return $result;
         //直接返回资源数组.
     } else {
         // Load the result as an associative array
         $result = $this->_db_builder->as_assoc()->execute($this->_db);
         $this->reset();
         if ($result->count() === 1) {
             // Load object values
             $this->_load_values($result->current());
         } else {
             // Clear the object, nothing was found
             $this->clear();
         }
         return $this;
     }
 }
Ejemplo n.º 2
0
 /**
  * Choose the fields(s) to select from.
  *
  *     $query->select_column('column');
  *     $query->select_column('field', 'alias');
  *     $query->select_column(array('column', 'column2', '...'));
  *
  * @param   string|array  $columns list of field names or actual columns
  * @param   string        $alias   An optional alias if passing a string for $columns
  * @return Jelly_Builder
  */
 public function select_column($columns, $alias = NULL)
 {
     // Allow passing a single argument
     if (!is_array($columns)) {
         // Check for an alias
         if ($alias) {
             $columns = array($columns, $alias);
         }
         $columns = array($columns);
     }
     foreach ($columns as $i => $column) {
         if (is_array($column)) {
             $columns[$i][0] = $this->_field_alias($column[0]);
         } else {
             // Check for * and model.*
             if (FALSE !== strpos($column, '*')) {
                 $meta = $this->_meta;
                 if ($column != '*') {
                     $meta = explode('.', $column);
                     $meta = Jelly::meta($meta[0]);
                 }
                 // Can we continue? Only if there's a valid meta object
                 if ($meta) {
                     $add = array();
                     foreach ($meta->fields() as $field) {
                         if ($field->in_db) {
                             $add[] = array($this->_field_alias($field->model . '.' . $field->name), $field->name);
                         } elseif ($field->column instanceof Database_Expression) {
                             $add[] = array($field->column, $field->name);
                         }
                     }
                     // Add these columns before we continue
                     parent::select_array($add);
                     // Remove the item we just added. It's no longer valid
                     unset($columns[$i]);
                     continue;
                 }
             }
             $columns[$i] = $this->_field_alias($column);
         }
     }
     return parent::select_array($columns);
 }