Class Database_Result_Cached

Database_Result_Cached

extends Kohana_Database_Result_Cached
extends Database_Result
extends Kohana_Database_Result

Implements: ArrayAccess | SeekableIterator | Traversable | Iterator | Countable

Object used for caching the results of select queries. See Results for usage and examples.

package
Kohana/Database
category
Query/Result
author
Kohana Team
copyright
(c) Kohana Team
license
https://koseven.ga/LICENSE.md


Information

This class is a transparent base class for Kohana_Database_Result_Cached

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 = NULL ] ) (defined in Kohana_Database_Result_Cached)

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 = NULL - $as_object

Return Values

  • void

Source Code

public function __construct(array $result, $sql, $as_object = NULL)
{
	parent::__construct($result, $sql, $as_object);

	// Find the number of rows in the result
	$this->_total_rows = count($result);
}

public __destruct() (defined in Kohana_Database_Result_Cached)

Result destruction cleans up all open result sets.

Return Values

  • void

Source Code

public function __destruct()
{
	// Cached results do not use resources
}

public cached() (defined in Kohana_Database_Result_Cached)

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

public current() (defined in Kohana_Database_Result_Cached)

Source Code

public function current()
{
	// Return an array of the row
	return $this->valid() ? $this->_result[$this->_current_row] : NULL;
}

public seek() (defined in Kohana_Database_Result_Cached)

Source Code

public function seek($offset)
{
	if ($this->offsetExists($offset))
	{
		$this->_current_row = $offset;

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

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

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

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

Do you want to contribute to Koseven?

We need YOUR help!

This project is open source. What does this mean? YOU can help:
  • Found a bug? Report it on Github
  • Need a feature? Add it Here
  • Want to help? Join the Forum
Go to Github