Class yii\db\DataReader

Inheritanceyii\db\DataReader » yii\base\BaseObject
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

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

Public Methods

Hide inherited methods

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

Property Details

Hide inherited properties

$columnCount public property

The number of columns in the result set.

public integer $columnCount null
$fetchMode public property

Fetch mode.

public integer $fetchMode null
$isClosed public property

Whether the reader is closed or not.

public boolean $isClosed null
$rowCount public property

Number of rows contained in the result.

public integer $rowCount null

Method Details

Hide inherited methods

__call() public method

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()");
}

            
__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

                public function __construct(Command $command, $config = [])
{
    $this->_statement = $command->pdoStatement;
    $this->_statement->setFetchMode(\PDO::FETCH_ASSOC);
    parent::__construct($config);
}

            
__get() public method

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);
}

            
__isset() public method

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.

See also https://www.php.net/manual/en/function.isset.php.

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;
}

            
__set() public method

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);
    }
}

            
__unset() public method

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.

See also https://www.php.net/manual/en/function.unset.php.

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);
    }
}

            
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 https://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|null

Data type of the parameter

                public function bindColumn($column, &$value, $dataType = null)
{
    if ($dataType === null) {
        $this->_statement->bindColumn($column, $value);
    } else {
        $this->_statement->bindColumn($column, $value, $dataType);
    }
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

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);

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);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

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);

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);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::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();
}

            
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 ( )

                public function close()
{
    $this->_statement->closeCursor();
    $this->_closed = true;
}

            
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.

                #[\ReturnTypeWillChange]
public function count()
{
    return $this->getRowCount();
}

            
current() public method

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;
}

            
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.

                public function getColumnCount()
{
    return $this->_statement->columnCount();
}

            
getIsClosed() public method

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;
}

            
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.

                public function getRowCount()
{
    return $this->_statement->rowCount();
}

            
hasMethod() public method

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);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

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);

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);
}

            
init() public method

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 void init ( )

                public function init()
{
}

            
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.

                #[\ReturnTypeWillChange]
public function key()
{
    return $this->_index;
}

            
next() public method

Moves the internal pointer to the next row.

This method is required by the interface Iterator.

public void next ( )

                #[\ReturnTypeWillChange]
public function next()
{
    $this->_row = $this->_statement->fetch();
    $this->_index++;
}

            
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.

                public function nextResult()
{
    if (($result = $this->_statement->nextRowset()) !== false) {
        $this->_index = -1;
    }
    return $result;
}

            
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

                public function read()
{
    return $this->_statement->fetch();
}

            
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.

                public function readAll()
{
    return $this->_statement->fetchAll();
}

            
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

                public function readColumn($columnIndex)
{
    return $this->_statement->fetchColumn($columnIndex);
}

            
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

                public function readObject($className, $fields)
{
    return $this->_statement->fetchObject($className, $fields);
}

            
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

                #[\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.');
    }
}

            
setFetchMode() public method

Set the default fetch mode for this statement.

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

public void setFetchMode ( $mode )
$mode integer

Fetch mode

                public function setFetchMode($mode)
{
    $params = func_get_args();
    call_user_func_array([$this->_statement, 'setFetchMode'], $params);
}

            
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.

                #[\ReturnTypeWillChange]
public function valid()
{
    return $this->_row !== false;
}