Class yii\db\DataReader

Inheritanceyii\db\DataReader » yii\base\Object
ImplementsCountable, Iterator, yii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/db/DataReader.php

DataReader represents a forward-only stream of rows from a query result set.

To read the current row of data, call read(). The method readAll() returns all the rows in a single array. Rows of data can also be read by iterating through the reader. For example,

$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.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$columnCount integer The number of columns in the result set. yii\db\DataReader
$fetchMode integer Fetch mode yii\db\DataReader
$isClosed boolean Whether the reader is closed or not. yii\db\DataReader
$rowCount integer Number of rows contained in the result. yii\db\DataReader

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Object
__construct() Constructor. yii\db\DataReader
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
bindColumn() Binds a column to a PHP variable. yii\db\DataReader
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
close() Closes the reader. yii\db\DataReader
count() Returns the number of rows in the result set. yii\db\DataReader
current() Returns the current row. yii\db\DataReader
getColumnCount() Returns the number of columns in the result set. yii\db\DataReader
getIsClosed() Whether the reader is closed or not. yii\db\DataReader
getRowCount() Returns the number of rows in the result set. yii\db\DataReader
hasMethod() Returns a value indicating whether a method is defined. yii\base\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes the object. yii\base\Object
key() Returns the index of the current row. yii\db\DataReader
next() Moves the internal pointer to the next row. yii\db\DataReader
nextResult() Advances the reader to the next result when reading the results of a batch of statements. yii\db\DataReader
read() Advances the reader to the next row in a result set. yii\db\DataReader
readAll() Reads the whole result set into an array. yii\db\DataReader
readColumn() Returns a single column from the next row of a result set. yii\db\DataReader
readObject() Returns an object populated with the next row of data. yii\db\DataReader
rewind() Resets the iterator to the initial state. yii\db\DataReader
setFetchMode() Set the default fetch mode for this statement yii\db\DataReader
valid() Returns whether there is a row of data at current position. yii\db\DataReader

Property Details

$columnCount public read-only property

The number of columns in the result set.

$fetchMode public write-only property

Fetch mode

public void setFetchMode ( $mode )
$isClosed public read-only property

Whether the reader is closed or not.

public boolean getIsClosed ( )
$rowCount public read-only property

Number of rows contained in the result.

public integer getRowCount ( )

Method Details

__construct() public method

Constructor.

public void __construct ( yii\db\Command $command, $config = [] )
$command yii\db\Command

The command generating the query result

$config array

Name-value pairs that will be used to initialize the object properties

bindColumn() public method

Binds a column to a PHP variable.

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 void bindColumn ( $column, &$value, $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

Closes the reader.

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

public void close ( )
count() public method

Returns the number of rows in the result set.

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 integer count ( )
return integer

Number of rows contained in the result.

current() public method

Returns the current row.

This method is required by the interface Iterator.

public mixed current ( )
return mixed

The current row.

getColumnCount() public method

Returns the number of columns in the result set.

Note, even there's no row in the reader, this still gives correct column number.

public integer getColumnCount ( )
return integer

The number of columns in the result set.

getIsClosed() public method

Whether the reader is closed or not.

public boolean getIsClosed ( )
return boolean

Whether the reader is closed or not.

getRowCount() public method

Returns the number of rows in the result set.

Note, most DBMS may not give a meaningful count. In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.

public integer getRowCount ( )
return integer

Number of rows contained in the result.

key() public method

Returns the index of the current row.

This method is required by the interface Iterator.

public integer key ( )
return integer

The index of the current row.

next() public method

Moves the internal pointer to the next row.

This method is required by the interface Iterator.

public void next ( )
nextResult() public method

Advances the reader to the next result when reading the results of a batch of statements.

This method is only useful when there are multiple result sets returned by the query. Not all DBMS support this feature.

public boolean nextResult ( )
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 array read ( )
return array

The current row, false if no more row available

readAll() public method

Reads the whole result set into an array.

public array readAll ( )
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 mixed readColumn ( $columnIndex )
$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 mixed readObject ( $className, $fields )
$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

Resets the iterator to the initial state.

This method is required by the interface Iterator.

public void rewind ( )
throws yii\base\InvalidCallException

if this method is invoked twice

setFetchMode() public method

Set the default fetch mode for this statement

See also http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php.

public void setFetchMode ( $mode )
$mode integer

Fetch mode

valid() public method

Returns whether there is a row of data at current position.

This method is required by the interface Iterator.

public boolean valid ( )
return boolean

Whether there is a row of data at current position.