Granada\ORM::find_one PHP Method

find_one() public method

As a shortcut, you may supply an ID as a parameter to this method. This will perform a primary key lookup on the table.
public find_one ( $id = null )
    public function find_one($id = null)
    {
        if (!is_null($id)) {
            $this->where_id_is($id);
        }
        $rows = $this->limit(1)->_run();
        if (empty($rows)) {
            return false;
        }
        return $this->_create_instance_from_row($rows[0]);
    }

Usage Example

Beispiel #1
0
 /**
  * Wrap Idiorm's find_one method to return
  * an instance of the class associated with
  * this wrapper instead of the raw ORM class.
  * Added: hidrate the model instance before returning
  * @param integer $id
  */
 public function find_one($id = null)
 {
     $result = $this->_create_model_instance(parent::find_one($id));
     if ($result) {
         // set result on an result set for the eager load to work
         $key = isset($result->{$this->_instance_id_column}) && $this->_associative_results ? $result->id() : 0;
         $results = array($key => $result);
         Eager::hydrate($this, $results, self::$_config[$this->_connection_name]['return_result_sets']);
         // return the result as element, not result set
         $result = $results[$key];
     }
     return $result;
 }
ORM