Inheritance | yii\db\DataReader » yii\base\BaseObject |
---|---|
Implements | Countable, Iterator, yii\base\Configurable |
Available since version | 2.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.
Property | Type | Description | Defined 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 |
Method | Description | Defined By |
---|---|---|
__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.
Defined in: yii\base\BaseObject::__call()
Calls the named method which is not a class method.
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.
public mixed __call ( $name, $params ) | ||
$name | string |
The method name |
$params | array |
Method parameters |
return | mixed |
The method return value |
---|---|---|
throws | yii\base\UnknownMethodException |
when calling unknown method |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
Constructor.
public function __construct(Command $command, $config = [])
{
$this->_statement = $command->pdoStatement;
$this->_statement->setFetchMode(\PDO::FETCH_ASSOC);
parent::__construct($config);
}
Defined in: yii\base\BaseObject::__get()
Returns the value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;
.
See also __set().
public mixed __get ( $name ) | ||
$name | string |
The property name |
return | mixed |
The property value |
---|---|---|
throws | yii\base\UnknownPropertyException |
if the property is not defined |
throws | yii\base\InvalidCallException |
if the property is write-only |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
Defined in: yii\base\BaseObject::__isset()
Checks if a property is set, i.e. defined and not null.
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.
public boolean __isset ( $name ) | ||
$name | string |
The property name or the event name |
return | boolean |
Whether the named property is set (not null). |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
Defined in: yii\base\BaseObject::__set()
Sets value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;
.
See also __get().
public void __set ( $name, $value ) | ||
$name | string |
The property name or the event name |
$value | mixed |
The property value |
throws | yii\base\UnknownPropertyException |
if the property is not defined |
---|---|---|
throws | yii\base\InvalidCallException |
if the property is read-only |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
Defined in: yii\base\BaseObject::__unset()
Sets an object property to null.
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.
public void __unset ( $name ) | ||
$name | string |
The property name |
throws | yii\base\InvalidCallException |
if the property is read only. |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
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 https://www.php.net/manual/en/function.PDOStatement-bindColumn.php.
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.
Name of the PHP variable to which the column will be bound.
public function bindColumn($column, &$value, $dataType = null)
{
if ($dataType === null) {
$this->_statement->bindColumn($column, $value);
} else {
$this->_statement->bindColumn($column, $value, $dataType);
}
}
Defined in: yii\base\BaseObject::canGetProperty()
Returns a value indicating whether a property can be read.
A property is readable if:
$checkVars
is true);See also canSetProperty().
public boolean canGetProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property can be read |
---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
Defined in: yii\base\BaseObject::canSetProperty()
Returns a value indicating whether a property can be set.
A property is writable if:
$checkVars
is true);See also canGetProperty().
public boolean canSetProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property can be written |
---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class
instead.
Defined in: yii\base\BaseObject::className()
Returns the fully qualified name of this class.
public static string className ( ) | ||
return | string |
The fully qualified name of this class. |
---|
public static function className()
{
return get_called_class();
}
Closes the reader.
This frees up the resources allocated for executing this SQL statement. Read attempts after this method call are unpredictable.
public function close()
{
$this->_statement->closeCursor();
$this->_closed = true;
}
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. |
---|
#[\ReturnTypeWillChange]
public function count()
{
return $this->getRowCount();
}
Returns the current row.
This method is required by the interface Iterator.
public mixed current ( ) | ||
return | mixed |
The current row. |
---|
#[\ReturnTypeWillChange]
public function current()
{
return $this->_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. |
---|
public function getColumnCount()
{
return $this->_statement->columnCount();
}
Whether the reader is closed or not.
public boolean getIsClosed ( ) | ||
return | boolean |
Whether the reader is closed or not. |
---|
public function getIsClosed()
{
return $this->_closed;
}
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. |
---|
public function getRowCount()
{
return $this->_statement->rowCount();
}
Defined in: yii\base\BaseObject::hasMethod()
Returns a value indicating whether a method is defined.
The default implementation is a call to php function method_exists()
.
You may override this method when you implemented the php magic method __call()
.
public boolean hasMethod ( $name ) | ||
$name | string |
The method name |
return | boolean |
Whether the method is defined |
---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
Defined in: yii\base\BaseObject::hasProperty()
Returns a value indicating whether a property is defined.
A property is defined if:
$checkVars
is true);See also:
public boolean hasProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property is defined |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
Defined in: yii\base\BaseObject::init()
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public function init()
{
}
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. |
---|
#[\ReturnTypeWillChange]
public function key()
{
return $this->_index;
}
Moves the internal pointer to the next row.
This method is required by the interface Iterator.
#[\ReturnTypeWillChange]
public function next()
{
$this->_row = $this->_statement->fetch();
$this->_index++;
}
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. |
---|
public function nextResult()
{
if (($result = $this->_statement->nextRowset()) !== false) {
$this->_index = -1;
}
return $result;
}
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 |
---|
public function read()
{
return $this->_statement->fetch();
}
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. |
---|
public function readAll()
{
return $this->_statement->fetchAll();
}
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 |
---|
public function readColumn($columnIndex)
{
return $this->_statement->fetchColumn($columnIndex);
}
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 |
---|
public function readObject($className, $fields)
{
return $this->_statement->fetchObject($className, $fields);
}
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 |
---|
#[\ReturnTypeWillChange]
public function rewind()
{
if ($this->_index < 0) {
$this->_row = $this->_statement->fetch();
$this->_index = 0;
} else {
throw new InvalidCallException('DataReader cannot rewind. It is a forward-only reader.');
}
}
Set the default fetch mode for this statement.
See also https://www.php.net/manual/en/function.PDOStatement-setFetchMode.php.
public function setFetchMode($mode)
{
$params = func_get_args();
call_user_func_array([$this->_statement, 'setFetchMode'], $params);
}