Database_Result
extends Kohana_Database_Result
Implements: ArrayAccess | SeekableIterator | Traversable | Iterator | Countable
Database result wrapper. See Results for usage and examples.
Information
This class is a transparent base class for Kohana_Database_Result
Constants
- None
Properties
-
protected $_as_object
-
Default value:
NULL
-
protected $_current_row
-
Default value:
integer 0
-
protected $_object_params
-
Default value:
NULL
-
protected $_query
-
Default value:
NULL
-
protected $_result
-
Default value:
NULL
-
protected $_total_rows
-
Default value:
integer 0
Methods
public __construct(mixed $result , string $sql [, mixed $as_object = bool FALSE , array $params = NULL ] ) (defined in Kohana_Database_Result)
Sets the total number of rows and stores the result locally.
Parameters
- mixed $result required - Query result
- string $sql required - SQL query
- mixed $as_object = bool FALSE - $as_object
- array $params = NULL - $params
Return Values
- void
Source Code
public function __construct($result, $sql, $as_object = FALSE, array $params = NULL)
{
// Store the result locally
$this->_result = $result;
// Store the SQL locally
$this->_query = $sql;
if (is_object($as_object))
{
// Get the object class name
$as_object = get_class($as_object);
}
// Results as objects or associative arrays
$this->_as_object = $as_object;
if ($params)
{
// Object constructor params
$this->_object_params = $params;
}
}
abstract public __destruct() (defined in Kohana_Database_Result)
Result destruction cleans up all open result sets.
Return Values
- void
Source Code
abstract public function __destruct();
public as_array([ string $key = NULL , string $value = NULL ] ) (defined in Kohana_Database_Result)
Return all of the rows in the result as an array.
// Indexed array of all rows
$rows = $result->as_array();
// Associative array of rows by "id"
$rows = $result->as_array('id');
// Associative array of rows, "id" => "name"
$rows = $result->as_array('id', 'name');
Parameters
- string $key = NULL - Column for associative keys
- string $value = NULL - Column for values
Return Values
- array
Source Code
public function as_array($key = NULL, $value = NULL)
{
$results = [];
if ($key === NULL AND $value === NULL)
{
// Indexed rows
foreach ($this as $row)
{
$results[] = $row;
}
}
elseif ($key === NULL)
{
// Indexed columns
if ($this->_as_object)
{
foreach ($this as $row)
{
$results[] = $row->$value;
}
}
else
{
foreach ($this as $row)
{
$results[] = $row[$value];
}
}
}
elseif ($value === NULL)
{
// Associative rows
if ($this->_as_object)
{
foreach ($this as $row)
{
$results[$row->$key] = $row;
}
}
else
{
foreach ($this as $row)
{
$results[$row[$key]] = $row;
}
}
}
else
{
// Associative columns
if ($this->_as_object)
{
foreach ($this as $row)
{
$results[$row->$key] = $row->$value;
}
}
else
{
foreach ($this as $row)
{
$results[$row[$key]] = $row[$value];
}
}
}
$this->rewind();
return $results;
}
public cached() (defined in Kohana_Database_Result)
Get a cached database result from the current result iterator.
$cachable = serialize($result->cached());
Tags
Return Values
- Database_Result_Cached
Source Code
public function cached()
{
return new Database_Result_Cached($this->as_array(), $this->_query, $this->_as_object);
}
public count() (defined in Kohana_Database_Result)
Implements Countable::count, returns the total number of rows.
echo count($result);
Return Values
- integer
Source Code
public function count()
{
return $this->_total_rows;
}
public get(string $name [, mixed $default = NULL ] ) (defined in Kohana_Database_Result)
Return the named column from the current row.
// Get the "id" value
$id = $result->get('id');
Parameters
- string $name required - Column to get
- mixed $default = NULL - Default value if the column does not exist
Return Values
- mixed
Source Code
public function get($name, $default = NULL)
{
$row = $this->current();
if ($this->_as_object)
{
if (isset($row->$name))
return $row->$name;
}
else
{
if (isset($row[$name]))
return $row[$name];
}
return $default;
}
public next() (defined in Kohana_Database_Result)
Implements Iterator::next, moves to the next row.
next($result);
Return Values
- $this
Source Code
public function next()
{
++$this->_current_row;
return $this;
}
public offsetExists(int $offset ) (defined in Kohana_Database_Result)
Implements ArrayAccess::offsetExists, determines if row exists.
if (isset($result[10]))
{
// Row 10 exists
}
Parameters
- int $offset required - $offset
Return Values
- boolean
Source Code
public function offsetExists($offset)
{
return ($offset >= 0 AND $offset < $this->_total_rows);
}
public offsetGet(int $offset ) (defined in Kohana_Database_Result)
Implements ArrayAccess::offsetGet, gets a given row.
$row = $result[10];
Parameters
- int $offset required - $offset
Return Values
- mixed
Source Code
public function offsetGet($offset)
{
if ( ! $this->seek($offset))
return NULL;
return $this->current();
}
abstract public current() (defined in Iterator)
public key() (defined in Kohana_Database_Result)
Implements Iterator::key, returns the current row number.
echo key($result);
Return Values
- integer
Source Code
public function key()
{
return $this->_current_row;
}
final public offsetSet(int $offset , mixed $value ) (defined in Kohana_Database_Result)
Implements ArrayAccess::offsetSet, throws an error.
You cannot modify a database result.
Parameters
- int $offset required - $offset
- mixed $value required - $value
Tags
Return Values
- void
Source Code
final public function offsetSet($offset, $value)
{
throw new Kohana_Exception('Database results are read-only');
}
final public offsetUnset(int $offset ) (defined in Kohana_Database_Result)
Implements ArrayAccess::offsetUnset, throws an error.
You cannot modify a database result.
Parameters
- int $offset required - $offset
Tags
Return Values
- void
Source Code
final public function offsetUnset($offset)
{
throw new Kohana_Exception('Database results are read-only');
}
public prev() (defined in Kohana_Database_Result)
Implements Iterator::prev, moves to the previous row.
prev($result);
Return Values
- $this
Source Code
public function prev()
{
--$this->_current_row;
return $this;
}
public rewind() (defined in Kohana_Database_Result)
Implements Iterator::rewind, sets the current row to zero.
rewind($result);
Return Values
- $this
Source Code
public function rewind()
{
$this->_current_row = 0;
return $this;
}
public valid() (defined in Kohana_Database_Result)
Implements Iterator::valid, checks if the current row exists.
This method is only used internally.
Return Values
- boolean
Source Code
public function valid()
{
return $this->offsetExists($this->_current_row);
}