/** * 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; } }
/** * Get a single row as an associative array. * * @param integer $id The ID of the row to get. * @return array */ public function get_row($id) { $query = new Database_Query_Builder_Select(); $query->from($this->get_name()); $query->limit(1); $pk_column = $this->get_pk_column(); $pk_name = !$pk_column ? 'id' : $pk_column->get_name(); $query->where($pk_name, '=', $id); $row = $query->execute($this->_db)->current(); return $row; }