Implements:
ArrayAccess | SeekableIterator | Traversable | Iterator | Countable
Database result wrapper. See Results for usage and examples.
- package
- Kohana/Database
- category
- Query/Result
- author
- Kohana Team
- copyright
- © 2008-2009 Kohana Team
- license
- http://kohanaphp.com/license
Class declared in MODPATH/database/classes/database/result.php on line 3.
public __construct( mixed $result , string $sql [, mixed $as_object = bool FALSE , array $params = NULL ] )
(defined in Kohana_Database_Result)
link to this
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
Source Code
public function __construct( $result , $sql , $as_object = FALSE, array $params = NULL)
{
$this ->_result = $result ;
$this ->_query = $sql ;
if ( is_object ( $as_object ))
{
$as_object = get_class( $as_object );
}
$this ->_as_object = $as_object ;
if ( $params )
{
$this ->_object_params = $params ;
}
}
|
Result destruction cleans up all open result sets.
Return Values
Source Code
abstract public function __destruct();
|
public as_array( [ string $key = NULL , string $value = NULL ] )
(defined in Kohana_Database_Result)
link to this
Return all of the rows in the result as an array.
$rows = $result ->as_array();
$rows = $result ->as_array( 'id' );
$rows = $result ->as_array( 'id' , 'name' );
|
Parameters
-
string
$key
= NULL - Column for associative keys
-
string
$value
= NULL - Column for values
Return Values
Source Code
public function as_array( $key = NULL, $value = NULL)
{
$results = array ();
if ( $key === NULL AND $value === NULL)
{
foreach ( $this as $row )
{
$results [] = $row ;
}
}
elseif ( $key === NULL)
{
if ( $this ->_as_object)
{
foreach ( $this as $row )
{
$results [] = $row -> $value ;
}
}
else
{
foreach ( $this as $row )
{
$results [] = $row [ $value ];
}
}
}
elseif ( $value === NULL)
{
if ( $this ->_as_object)
{
foreach ( $this as $row )
{
$results [ $row -> $key ] = $row ;
}
}
else
{
foreach ( $this as $row )
{
$results [ $row [ $key ]] = $row ;
}
}
}
else
{
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 ;
}
|
Get a cached database result from the current result iterator.
$cachable = serialize( $result ->cached());
|
Tags
Return Values
Source Code
public function cached()
{
return new Database_Result_Cached( $this ->as_array(), $this ->_query, $this ->_as_object);
}
|
Return Values
Source Code
public function count ()
{
return $this ->_total_rows;
}
|
Return the named column from the current row.
$id = $result ->get( 'id' );
|
Parameters
-
string
$name
required - Column to get
-
mixed
$default
= NULL - Default value if the column does not exist
Return Values
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 ;
}
|
Return Values
Source Code
public function key()
{
return $this ->_current_row;
}
|
Return Values
Source Code
public function next()
{
++ $this ->_current_row;
return $this ;
}
|
Parameters
-
int
$offset
required - $offset
Return Values
Source Code
public function offsetExists( $offset )
{
return ( $offset >= 0 AND $offset < $this ->_total_rows);
}
|
Parameters
-
int
$offset
required - $offset
Return Values
Source Code
public function offsetGet( $offset )
{
if ( ! $this ->seek( $offset ))
return NULL;
return $this ->current();
}
|
Parameters
-
int
$offset
required - $offset
-
mixed
$value
required - $value
Tags
Return Values
Source Code
final public function offsetSet( $offset , $value )
{
throw new Kohana_Exception( 'Database results are read-only' );
}
|
Parameters
-
int
$offset
required - $offset
Tags
Return Values
Source Code
final public function offsetUnset( $offset )
{
throw new Kohana_Exception( 'Database results are read-only' );
}
|
Return Values
Source Code
public function prev()
{
-- $this ->_current_row;
return $this ;
}
|
Return Values
Source Code
public function rewind ()
{
$this ->_current_row = 0;
return $this ;
}
|
Implements Iterator::valid, checks if the current row exists.
This method is only used internally.
Return Values
Source Code
public function valid()
{
return $this ->offsetExists( $this ->_current_row);
}
|