0 follower

Interface Yiisoft\Db\Query\DataReaderInterface

ExtendsCountable, Iterator
Implemented byYiisoft\Db\Driver\Pdo\PdoDataReader

This interface represents a forward-only stream of rows from a query result set.

To read the current row of data just iterate on it.

For example,

$command = $connection->createCommand('SELECT * FROM post');
$reader = $command->query();

foreach ($reader as $row) {
    $rows[] = $row;
}

Note: That since DataReader is a forward-only stream, you can only traverse it once. Doing it the second time will
throw an exception.

Public Methods

Hide inherited methods

Method Description Defined By
current() Returns the current row or false if there is no row at the current position. Yiisoft\Db\Query\DataReaderInterface
indexBy() Sets indexBy property. Yiisoft\Db\Query\DataReaderInterface
key() Returns the index of the current row or null if {@see indexBy} property is specified and there is no row at the current position. Yiisoft\Db\Query\DataReaderInterface
resultCallback() Sets the callback, to be called on all rows of the query result before returning them. Yiisoft\Db\Query\DataReaderInterface
typecastColumns() Sets the columns for type casting the query results. Yiisoft\Db\Query\DataReaderInterface

Method Details

Hide inherited methods

current() public abstract method

Returns the current row or false if there is no row at the current position.

This method is required by the interface {@see \Iterator}.

public abstract array|object|false current ( )

                public function current(): array|object|false;

            
indexBy() public abstract method

Sets indexBy property.

public abstract Yiisoft\Db\Query\DataReaderInterface indexBy ( Closure|string|null $indexBy )
$indexBy Closure|string|null

The name of the column by which the query results should be indexed by. This can also be a Closure instance (for example, anonymous function) that returns the index value based on the given row data.

The signature of the callable should be:

function (array $row): array-key
{
    // return the index value corresponding to $row
}

                public function indexBy(Closure|string|null $indexBy): static;

            
key() public abstract method

Returns the index of the current row or null if {@see indexBy} property is specified and there is no row at the current position.

This method is required by the interface {@see \Iterator}.

public abstract integer|string|null key ( )

                public function key(): int|string|null;

            
resultCallback() public abstract method

Sets the callback, to be called on all rows of the query result before returning them.

For example:

function (array $rows): array {
    foreach ($rows as &$row) {
        $row['name'] = strtoupper($row['name']);
    }
    return $rows;
}
public abstract Yiisoft\Db\Query\DataReaderInterface resultCallback ( Closure|null $resultCallback )
$resultCallback Closure|null

                public function resultCallback(?Closure $resultCallback): static;

            
typecastColumns() public abstract method

Sets the columns for type casting the query results.

Do not use this method if you want to get the raw data from the query.

public abstract Yiisoft\Db\Query\DataReaderInterface typecastColumns ( Yiisoft\Db\Schema\Column\ColumnInterface[] $typecastColumns )
$typecastColumns Yiisoft\Db\Schema\Column\ColumnInterface[]

                public function typecastColumns(array $typecastColumns): static;