0 follower

Final Class Yiisoft\Db\Driver\Pdo\PdoDataReader

InheritanceYiisoft\Db\Driver\Pdo\PdoDataReader
ImplementsYiisoft\Db\Query\DataReaderInterface

Provides an abstract way to read data from a database.

A data reader is an object that can be used to read a forward-only stream of rows from a database.

It's typically used in combination with a command object, such as a {@see \Yiisoft\Db\Command\AbstractCommand}, to execute a SELECT statement and read the results.

The class provides methods for accessing the data returned by the query.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( PDOStatement $statement )
$statement PDOStatement

The PDO statement object that contains the result of the query.

                public function __construct(private readonly PDOStatement $statement)
{
    /** @var array<string,mixed>|false */
    $this->row = $statement->fetch(PDO::FETCH_ASSOC);
}

            
__destruct() public method

public mixed __destruct ( )

                public function __destruct()
{
    $this->statement->closeCursor();
}

            
count() public method

Returns the number of rows in the result set.

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

Note, most DBMS mayn't give a meaningful count. In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.

public integer count ( )

                public function count(): int
{
    return $this->statement->rowCount();
}

            
current() public method

public array|object|false current ( )

                public function current(): array|object|false
{
    $row = $this->row;
    if ($row === false) {
        return false;
    }
    if (!empty($this->typecastColumns)) {
        foreach ($this->typecastColumns as $key => $column) {
            $row[$key] = $column->phpTypecast($row[$key]);
        }
    }
    if ($this->resultCallback === null) {
        return $row;
    }
    return ($this->resultCallback)($row);
}

            
indexBy() public method

public Yiisoft\Db\Driver\Pdo\PdoDataReader indexBy ( Closure|string|null $indexBy )
$indexBy Closure|string|null

                public function indexBy(Closure|string|null $indexBy): static
{
    $this->indexBy = $indexBy;
    return $this;
}

            
key() public method

public integer|string|null key ( )

                public function key(): int|string|null
{
    if ($this->indexBy === null) {
        return $this->index;
    }
    if ($this->row === false) {
        return null;
    }
    if (is_string($this->indexBy)) {
        return (string) $this->row[$this->indexBy];
    }
    return ($this->indexBy)($this->row);
}

            
next() public method

Moves the internal pointer to the next row.

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

public void next ( )

                public function next(): void
{
    /** @var array<string,mixed>|false */
    $this->row = $this->statement->fetch(PDO::FETCH_ASSOC);
    $this->index++;
}

            
resultCallback() public method

public Yiisoft\Db\Driver\Pdo\PdoDataReader resultCallback ( Closure|null $resultCallback )
$resultCallback Closure|null

                public function resultCallback(?Closure $resultCallback): static
{
    $this->resultCallback = $resultCallback;
    return $this;
}

            
rewind() public method

Resets the iterator to the initial state.

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

public void rewind ( )
throws Yiisoft\Db\Exception\InvalidCallException

If the data reader isn't at the beginning.

                public function rewind(): void
{
    if ($this->index === 0) {
        return;
    }
    throw new InvalidCallException('DataReader cannot rewind. It is a forward-only reader.');
}

            
typecastColumns() public method

public Yiisoft\Db\Driver\Pdo\PdoDataReader typecastColumns ( array $typecastColumns )
$typecastColumns array

                public function typecastColumns(array $typecastColumns): static
{
    $this->typecastColumns = $typecastColumns;
    return $this;
}

            
valid() public method

Returns whether there is a row of data at current position.

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

public boolean valid ( )

                public function valid(): bool
{
    return $this->row !== false;
}