예제 #1
0
파일: orm.php 프로젝트: lz1988/stourwebcms
 /**
  * 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;
     }
 }
예제 #2
0
 /**
  *
  * @param Database_Query_Builder_Select $query
  */
 public function apply_ordering(&$query)
 {
     $this->orderby = Arr::get($_GET, 'orderby', '');
     $this->orderdir = Arr::get($_GET, 'orderdir') == 'desc' ? 'desc' : 'asc';
     if (!in_array($this->orderby, array_keys($this->get_columns()))) {
         $this->orderby = $this->get_title_column()->get_name();
     }
     if ($this->get_column($this->orderby)->is_foreign_key()) {
         $fk1_alias = 'o1';
         $fk1_table = $this->get_column($this->orderby)->get_referenced_table();
         $query->join(array($fk1_table->get_name(), $fk1_alias), 'LEFT OUTER');
         $query->on($this->get_name() . '.' . $this->orderby, '=', "{$fk1_alias}.id");
         $orderby = $fk1_alias . '.' . $fk1_table->get_title_column()->get_name();
         if ($fk1_table->get_title_column()->is_foreign_key()) {
             $fk2_alias = 'o2';
             $fk2_table = $fk1_table->get_title_column()->get_referenced_table();
             $query->join(array($fk2_table->get_name(), $fk2_alias), 'LEFT OUTER');
             $query->on($fk1_alias . '.' . $fk1_table->get_title_column()->get_name(), '=', "{$fk2_alias}.id");
             $orderby = $fk2_alias . '.' . $fk2_table->get_title_column()->get_name();
         }
         $query->order_by($orderby, $this->orderdir);
     } else {
         $query->order_by($this->get_name() . '.' . $this->orderby, $this->orderdir);
     }
 }
예제 #3
0
 /**
  * Applies sorting with "ORDER BY ..."
  *
  * @param   mixed   $column     column name or array($column, $alias) or object
  * @param   string  $direction  direction of sorting
  * @return Jelly_Builder
  */
 public function order_by($column, $direction = NULL)
 {
     return parent::order_by($this->_field_alias($column), $direction);
 }