Final Class Yiisoft\Db\Driver\Pdo\PdoDataReader
| Inheritance | Yiisoft\Db\Driver\Pdo\PdoDataReader |
|---|---|
| Implements | Yiisoft\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.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Db\Driver\Pdo\PdoDataReader | |
| __destruct() | Yiisoft\Db\Driver\Pdo\PdoDataReader | |
| count() | Returns the number of rows in the result set. | Yiisoft\Db\Driver\Pdo\PdoDataReader |
| current() | Yiisoft\Db\Driver\Pdo\PdoDataReader | |
| indexBy() | Yiisoft\Db\Driver\Pdo\PdoDataReader | |
| key() | Yiisoft\Db\Driver\Pdo\PdoDataReader | |
| next() | Moves the internal pointer to the next row. | Yiisoft\Db\Driver\Pdo\PdoDataReader |
| resultCallback() | Yiisoft\Db\Driver\Pdo\PdoDataReader | |
| rewind() | Resets the iterator to the initial state. | Yiisoft\Db\Driver\Pdo\PdoDataReader |
| typecastColumns() | Yiisoft\Db\Driver\Pdo\PdoDataReader | |
| valid() | Returns whether there is a row of data at current position. | Yiisoft\Db\Driver\Pdo\PdoDataReader |
Method Details
| 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);
}
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();
}
| 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);
}
| 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;
}
| 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);
}
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++;
}
| public Yiisoft\Db\Driver\Pdo\PdoDataReader resultCallback ( Closure|null $resultCallback ) | ||
| $resultCallback | Closure|null | |
public function resultCallback(?Closure $resultCallback): static
{
$this->resultCallback = $resultCallback;
return $this;
}
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.');
}
| public Yiisoft\Db\Driver\Pdo\PdoDataReader typecastColumns ( array $typecastColumns ) | ||
| $typecastColumns | array | |
public function typecastColumns(array $typecastColumns): static
{
$this->typecastColumns = $typecastColumns;
return $this;
}
Signup or Login in order to comment.