Modules

abstract Database_Result
extends Kohana_Database_Result

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.

Properties

protected $_as_object

protected $_current_row

protected $_object_params

protected $_query

protected $_result

protected $_total_rows

Methods

public __construct( mixed $result , string $sql [, $as_object = bool FALSE , $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
  • unknown $as_object = bool FALSE
  • unknown $params = NULL

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 = array();

	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;
}

abstract public seek( ) (defined in SeekableIterator)

public cached( ) (defined in Kohana_Database_Result)

Get a cached database result from the current result iterator.

$cachable = serialize($result->cached());

Tags

  • Since - 3.0.5

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 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;
}

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;
}

abstract public current( ) (defined in Iterator)

public offsetExists( ) (defined in Kohana_Database_Result)

Implements ArrayAccess::offsetExists, determines if row exists.

if (isset($result[10]))
{
    // Row 10 exists
}

Return Values

  • boolean

Source Code

public function offsetExists($offset)
{
	return ($offset >= 0 AND $offset < $this->_total_rows);
}

public offsetGet( ) (defined in Kohana_Database_Result)

Implements ArrayAccess::offsetGet, gets a given row.

$row = $result[10];

Return Values

  • mixed

Source Code

public function offsetGet($offset)
{
	if ( ! $this->seek($offset))
		return NULL;

	return $this->current();
}

final public offsetSet( ) (defined in Kohana_Database_Result)

Implements ArrayAccess::offsetSet, throws an error.

You cannot modify a database result.

Tags

Return Values

  • void

Source Code

final public function offsetSet($offset, $value)
{
	throw new Kohana_Exception('Database results are read-only');
}

final public offsetUnset( ) (defined in Kohana_Database_Result)

Implements ArrayAccess::offsetUnset, throws an error.

You cannot modify a database result.

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);
}