PHP Class yii\db\DataReader

To read the current row of data, call DataReader::read. The method DataReader::readAll returns all the rows in a single array. Rows of data can also be read by iterating through the reader. For example, php $command = $connection->createCommand('SELECT * FROM post'); $reader = $command->query(); while ($row = $reader->read()) { $rows[] = $row; } equivalent to: foreach ($reader as $row) { $rows[] = $row; } equivalent to: $rows = $reader->readAll(); Note that since DataReader is a forward-only stream, you can only traverse it once. Doing it the second time will throw an exception. It is possible to use a specific mode of data fetching by setting [[fetchMode]]. See the PHP manual for more details about possible fetch mode.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Object, implements Iterator, implements Countable
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Methods

Method Description
__construct ( Command $command, array $config = [] ) Constructor.
bindColumn ( integer | string $column, mixed &$value, integer $dataType = null ) Binds a column to a PHP variable.
close ( ) Closes the reader.
count ( ) : integer Returns the number of rows in the result set.
current ( ) : mixed Returns the current row.
getColumnCount ( ) : integer Returns the number of columns in the result set.
getIsClosed ( ) : boolean whether the reader is closed or not.
getRowCount ( ) : integer Returns the number of rows in the result set.
key ( ) : integer Returns the index of the current row.
next ( ) Moves the internal pointer to the next row.
nextResult ( ) : boolean Advances the reader to the next result when reading the results of a batch of statements.
read ( ) : array Advances the reader to the next row in a result set.
readAll ( ) : array Reads the whole result set into an array.
readColumn ( integer $columnIndex ) : mixed Returns a single column from the next row of a result set.
readObject ( string $className, array $fields ) : mixed Returns an object populated with the next row of data.
rewind ( ) Resets the iterator to the initial state.
setFetchMode ( integer $mode ) Set the default fetch mode for this statement
valid ( ) : boolean Returns whether there is a row of data at current position.

Method Details

__construct() public method

Constructor.
public __construct ( Command $command, array $config = [] )
$command Command the command generating the query result
$config array name-value pairs that will be used to initialize the object properties

bindColumn() public method

When rows of data are being fetched, the corresponding column value will be set in the variable. Note, the fetch mode must include PDO::FETCH_BOUND.
See also: http://www.php.net/manual/en/function.PDOStatement-bindColumn.php
public bindColumn ( integer | string $column, mixed &$value, integer $dataType = null )
$column integer | string Number of the column (1-indexed) or name of the column in the result set. If using the column name, be aware that the name should match the case of the column, as returned by the driver.
$value mixed Name of the PHP variable to which the column will be bound.
$dataType integer Data type of the parameter

close() public method

This frees up the resources allocated for executing this SQL statement. Read attempts after this method call are unpredictable.
public close ( )

count() public method

This method is required by the Countable interface. Note, most DBMS may not give a meaningful count. In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
public count ( ) : integer
return integer number of rows contained in the result.

current() public method

This method is required by the interface [[\Iterator]].
public current ( ) : mixed
return mixed the current row.

getColumnCount() public method

Note, even there's no row in the reader, this still gives correct column number.
public getColumnCount ( ) : integer
return integer the number of columns in the result set.

getIsClosed() public method

whether the reader is closed or not.
public getIsClosed ( ) : boolean
return boolean whether the reader is closed or not.

getRowCount() public method

Note, most DBMS may not give a meaningful count. In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
public getRowCount ( ) : integer
return integer number of rows contained in the result.

key() public method

This method is required by the interface [[\Iterator]].
public key ( ) : integer
return integer the index of the current row.

next() public method

This method is required by the interface [[\Iterator]].
public next ( )

nextResult() public method

This method is only useful when there are multiple result sets returned by the query. Not all DBMS support this feature.
public nextResult ( ) : boolean
return boolean Returns true on success or false on failure.

read() public method

Advances the reader to the next row in a result set.
public read ( ) : array
return array the current row, false if no more row available

readAll() public method

Reads the whole result set into an array.
public readAll ( ) : array
return array the result set (each array element represents a row of data). An empty array will be returned if the result contains no row.

readColumn() public method

Returns a single column from the next row of a result set.
public readColumn ( integer $columnIndex ) : mixed
$columnIndex integer zero-based column index
return mixed the column of the current row, false if no more rows available

readObject() public method

Returns an object populated with the next row of data.
public readObject ( string $className, array $fields ) : mixed
$className string class name of the object to be created and populated
$fields array Elements of this array are passed to the constructor
return mixed the populated object, false if no more row of data available

rewind() public method

This method is required by the interface [[\Iterator]].
public rewind ( )

setFetchMode() public method

Set the default fetch mode for this statement
See also: http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php
public setFetchMode ( integer $mode )
$mode integer fetch mode

valid() public method

This method is required by the interface [[\Iterator]].
public valid ( ) : boolean
return boolean whether there is a row of data at current position.