Class yii\db\DataReader
继承 | yii\db\DataReader » yii\base\BaseObject |
---|---|
实现 | Countable, Iterator, yii\base\Configurable |
可用版本自 | 2.0 |
源码 | https://github.com/yiichina/yii2/blob/api/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.
公共属性
属性 | 类型 | 描述 | 被定义在 |
---|---|---|---|
$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 |
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\BaseObject |
__construct() | Constructor. | yii\db\DataReader |
__get() | Returns the value of an object property. | yii\base\BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
__set() | Sets value of an object property. | yii\base\BaseObject |
__unset() | Sets an object property to null. | yii\base\BaseObject |
bindColumn() | Binds a column to a PHP variable. | yii\db\DataReader |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
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\BaseObject |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
init() | Initializes the object. | yii\base\BaseObject |
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 |
属性详情
The number of columns in the result set.
Fetch mode
Whether the reader is closed or not.
Number of rows contained in the result.
方法详情
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 |
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.
参见 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 |
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() |
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. |
---|
Returns the current row.
This method is required by the interface Iterator.
public mixed current() | ||
return | mixed | The current row. |
---|
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. |
---|
Whether the reader is closed or not.
public boolean getIsClosed() | ||
return | boolean | Whether the reader is closed or not. |
---|
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. |
---|
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. |
---|
Moves the internal pointer to the next row.
This method is required by the interface Iterator.
public void next() |
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. |
---|
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 |
---|
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. |
---|
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 |
---|
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 |
---|
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 |
---|
Set the default fetch mode for this statement.
参见 http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php.
public void setFetchMode($mode) | ||
$mode | integer | Fetch mode |
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. |
---|