DataReader
extends BaseObject
in package
implements
Iterator, Countable
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.
Tags
Table of Contents
Interfaces
- Iterator
- Countable
Properties
- $columnCount : int
- $fetchMode : int
- $isClosed : bool
- $rowCount : int
- $_closed : mixed
- $_index : mixed
- $_row : mixed
- $_statement : PDOStatement
Methods
- __call() : mixed
- Calls the named method which is not a class method.
- __construct() : mixed
- Constructor.
- __get() : mixed
- Returns the value of an object property.
- __isset() : bool
- Checks if a property is set, i.e. defined and not null.
- __set() : mixed
- Sets value of an object property.
- __unset() : mixed
- Sets an object property to null.
- bindColumn() : mixed
- Binds a column to a PHP variable.
- canGetProperty() : bool
- Returns a value indicating whether a property can be read.
- canSetProperty() : bool
- Returns a value indicating whether a property can be set.
- className() : string
- Returns the fully qualified name of this class.
- close() : mixed
- Closes the reader.
- count() : int
- Returns the number of rows in the result set.
- current() : mixed
- Returns the current row.
- getColumnCount() : int
- Returns the number of columns in the result set.
- getIsClosed() : bool
- whether the reader is closed or not.
- getRowCount() : int
- Returns the number of rows in the result set.
- hasMethod() : bool
- Returns a value indicating whether a method is defined.
- hasProperty() : bool
- Returns a value indicating whether a property is defined.
- init() : mixed
- Initializes the object.
- key() : int
- Returns the index of the current row.
- next() : mixed
- Moves the internal pointer to the next row.
- nextResult() : bool
- Advances the reader to the next result when reading the results of a batch of statements.
- read() : array<string|int, mixed>
- Advances the reader to the next row in a result set.
- readAll() : array<string|int, mixed>
- Reads the whole result set into an array.
- readColumn() : mixed
- Returns a single column from the next row of a result set.
- readObject() : mixed
- Returns an object populated with the next row of data.
- rewind() : mixed
- Resets the iterator to the initial state.
- setFetchMode() : mixed
- Set the default fetch mode for this statement.
- valid() : bool
- Returns whether there is a row of data at current position.
Properties
$columnCount read-only
public
int
$columnCount
The number of columns in the result set.
$fetchMode write-only
public
int
$fetchMode
Fetch mode.
$isClosed read-only
public
bool
$isClosed
Whether the reader is closed or not.
$rowCount read-only
public
int
$rowCount
Number of rows contained in the result.
$_closed
private
mixed
$_closed
= false
$_index
private
mixed
$_index
= -1
$_row
private
mixed
$_row
$_statement
private
PDOStatement
$_statement
the PDOStatement associated with the command
Methods
__call()
Calls the named method which is not a class method.
public
__call(string $name, array<string|int, mixed> $params) : mixed
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
Parameters
- $name : string
-
the method name
- $params : array<string|int, mixed>
-
method parameters
Tags
Return values
mixed —the method return value
__construct()
Constructor.
public
__construct(Command $command[, array<string|int, mixed> $config = [] ]) : mixed
Parameters
- $command : Command
-
the command generating the query result
- $config : array<string|int, mixed> = []
-
name-value pairs that will be used to initialize the object properties
__get()
Returns the value of an object property.
public
__get(string $name) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;
.
Parameters
- $name : string
-
the property name
Tags
Return values
mixed —the property value
__isset()
Checks if a property is set, i.e. defined and not null.
public
__isset(string $name) : bool
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($object->property)
.
Note that if the property is not defined, false will be returned.
Parameters
- $name : string
-
the property name or the event name
Tags
Return values
bool —whether the named property is set (not null).
__set()
Sets value of an object property.
public
__set(string $name, mixed $value) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;
.
Parameters
- $name : string
-
the property name or the event name
- $value : mixed
-
the property value
Tags
__unset()
Sets an object property to null.
public
__unset(string $name) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($object->property)
.
Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.
Parameters
- $name : string
-
the property name
Tags
bindColumn()
Binds a column to a PHP variable.
public
bindColumn(int|string $column, mixed &$value[, int|null $dataType = null ]) : mixed
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
- $column : int|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 : int|null = null
-
Data type of the parameter
Tags
canGetProperty()
Returns a value indicating whether a property can be read.
public
canGetProperty(string $name[, bool $checkVars = true ]) : bool
A property is readable if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property can be read
canSetProperty()
Returns a value indicating whether a property can be set.
public
canSetProperty(string $name[, bool $checkVars = true ]) : bool
A property is writable if:
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property can be written
className()
Returns the fully qualified name of this class.
public
static className() : string
Tags
Return values
string —the fully qualified name of this class.
close()
Closes the reader.
public
close() : mixed
This frees up the resources allocated for executing this SQL statement. Read attempts after this method call are unpredictable.
count()
Returns the number of rows in the result set.
public
count() : int
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.
Attributes
Return values
int —number of rows contained in the result.
current()
Returns the current row.
public
current() : mixed
This method is required by the interface [[\Iterator]].
Attributes
Return values
mixed —the current row.
getColumnCount()
Returns the number of columns in the result set.
public
getColumnCount() : int
Note, even there's no row in the reader, this still gives correct column number.
Return values
int —the number of columns in the result set.
getIsClosed()
whether the reader is closed or not.
public
getIsClosed() : bool
Return values
bool —whether the reader is closed or not.
getRowCount()
Returns the number of rows in the result set.
public
getRowCount() : int
Note, most DBMS may not give a meaningful count. In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
Return values
int —number of rows contained in the result.
hasMethod()
Returns a value indicating whether a method is defined.
public
hasMethod(string $name) : bool
The default implementation is a call to php function method_exists()
.
You may override this method when you implemented the php magic method __call()
.
Parameters
- $name : string
-
the method name
Return values
bool —whether the method is defined
hasProperty()
Returns a value indicating whether a property is defined.
public
hasProperty(string $name[, bool $checkVars = true ]) : bool
A property is defined if:
- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property is defined
init()
Initializes the object.
public
init() : mixed
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
key()
Returns the index of the current row.
public
key() : int
This method is required by the interface [[\Iterator]].
Attributes
Return values
int —the index of the current row.
next()
Moves the internal pointer to the next row.
public
next() : mixed
This method is required by the interface [[\Iterator]].
Attributes
nextResult()
Advances the reader to the next result when reading the results of a batch of statements.
public
nextResult() : bool
This method is only useful when there are multiple result sets returned by the query. Not all DBMS support this feature.
Return values
bool —Returns true on success or false on failure.
read()
Advances the reader to the next row in a result set.
public
read() : array<string|int, mixed>
Return values
array<string|int, mixed> —the current row, false if no more row available
readAll()
Reads the whole result set into an array.
public
readAll() : array<string|int, mixed>
Return values
array<string|int, mixed> —the result set (each array element represents a row of data). An empty array will be returned if the result contains no row.
readColumn()
Returns a single column from the next row of a result set.
public
readColumn(int $columnIndex) : mixed
Parameters
- $columnIndex : int
-
zero-based column index
Return values
mixed —the column of the current row, false if no more rows available
readObject()
Returns an object populated with the next row of data.
public
readObject(string $className, array<string|int, mixed> $fields) : mixed
Parameters
- $className : string
-
class name of the object to be created and populated
- $fields : array<string|int, mixed>
-
Elements of this array are passed to the constructor
Return values
mixed —the populated object, false if no more row of data available
rewind()
Resets the iterator to the initial state.
public
rewind() : mixed
This method is required by the interface [[\Iterator]].
Tags
Attributes
setFetchMode()
Set the default fetch mode for this statement.
public
setFetchMode(int $mode) : mixed
Parameters
- $mode : int
-
fetch mode
Tags
valid()
Returns whether there is a row of data at current position.
public
valid() : bool
This method is required by the interface [[\Iterator]].
Attributes
Return values
bool —whether there is a row of data at current position.