DataReader
API reference › yii › db › DataReader
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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$columnCount read-only | int | The number of columns in the result set. | DataReader |
$fetchMode write-only | int | Fetch mode. | DataReader |
$isClosed read-only | bool | Whether the reader is closed or not. | DataReader |
$rowCount read-only | int | Number of rows contained in the result. | DataReader |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | DataReader |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
bindColumn() | Binds a column to a PHP variable. | DataReader |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
close() | Closes the reader. | DataReader |
count() | Returns the number of rows in the result set. | DataReader |
current() | Returns the current row. | DataReader |
getColumnCount() | Returns the number of columns in the result set. | DataReader |
getIsClosed() | Whether the reader is closed or not. | DataReader |
getRowCount() | Returns the number of rows in the result set. | DataReader |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
key() | Returns the index of the current row. | DataReader |
next() | Moves the internal pointer to the next row. | DataReader |
nextResult() | Advances the reader to the next result when reading the results of a batch of statements. | DataReader |
read() | Advances the reader to the next row in a result set. | DataReader |
readAll() | Reads the whole result set into an array. | DataReader |
readColumn() | Returns a single column from the next row of a result set. | DataReader |
readObject() | Returns an object populated with the next row of data. | DataReader |
rewind() | Resets the iterator to the initial state. | DataReader |
setFetchMode() | Set the default fetch mode for this statement. | DataReader |
valid() | Returns whether there is a row of data at current position. | DataReader |
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(yii\db\Command $command, array $config = [])Constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$command | Command | The command generating the query result |
$config | array | Name-value pairs that will be used to initialize the object properties |
bindColumn()
Section titled “bindColumn() ”public function bindColumn(int|string $column, mixed &$value, int|null $dataType = null): mixedBinds 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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$column | int| | 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 | int| | Data type of the parameter |
Returns
mixed
See also
close()
Section titled “close() ”public function close(): mixedCloses the reader.
This frees up the resources allocated for executing this SQL statement. Read attempts after this method call are unpredictable.
Returns
mixed
count()
Section titled “count() ”public function count(): intReturns 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.
Returns
int: Number of rows contained in the result.
current()
Section titled “current() ”public function current(): mixedReturns the current row.
This method is required by the interface Iterator.
Returns
mixed: The current row.
getColumnCount()
Section titled “getColumnCount() ”public function getColumnCount(): intReturns the number of columns in the result set.
Note, even there’s no row in the reader, this still gives correct column number.
Returns
int: The number of columns in the result set.
getIsClosed()
Section titled “getIsClosed() ”public function getIsClosed(): boolWhether the reader is closed or not.
Returns
bool: Whether the reader is closed or not.
getRowCount()
Section titled “getRowCount() ”public function getRowCount(): intReturns 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.
Returns
int: Number of rows contained in the result.
key()
Section titled “key() ”public function key(): intReturns the index of the current row.
This method is required by the interface Iterator.
Returns
int: The index of the current row.
next()
Section titled “next() ”public function next(): mixedMoves the internal pointer to the next row.
This method is required by the interface Iterator.
Returns
mixed
nextResult()
Section titled “nextResult() ”public function nextResult(): boolAdvances 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.
Returns
bool: Returns true on success or false on failure.
read()
Section titled “read() ”public function read(): array|falseAdvances the reader to the next row in a result set.
Returns
array|false: The current row, false if no more row available
readAll()
Section titled “readAll() ”public function readAll(): arrayReads the whole result set into an array.
Returns
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()
Section titled “readColumn() ”public function readColumn(int $columnIndex): mixedReturns a single column from the next row of a result set.
Parameters
| Parameter | Type | Description |
|---|---|---|
$columnIndex | int | Zero-based column index |
Returns
mixed: The column of the current row, false if no more rows available
readObject()
Section titled “readObject() ”public function readObject(string $className, array $fields): mixedReturns an object populated with the next row of data.
Parameters
| Parameter | Type | Description |
|---|---|---|
$className | string | Class name of the object to be created and populated |
$fields | array | Elements of this array are passed to the constructor |
Returns
mixed: The populated object, false if no more row of data available
rewind()
Section titled “rewind() ”public function rewind(): mixedResets the iterator to the initial state.
This method is required by the interface Iterator.
setFetchMode()
Section titled “setFetchMode() ”public function setFetchMode(int $mode): mixedSet the default fetch mode for this statement.
Parameters
| Parameter | Type | Description |
|---|---|---|
$mode | int | Fetch mode |
Returns
mixed
See also
valid()
Section titled “valid() ”public function valid(): boolReturns whether there is a row of data at current position.
This method is required by the interface Iterator.
Returns
bool: Whether there is a row of data at current position.