Class Yiisoft\Data\Db\QueryDataReader
| Inheritance | Yiisoft\Data\Db\QueryDataReader |
|---|---|
| Implements | Yiisoft\Data\Db\QueryDataReaderInterface |
Public Methods
Method Details
| public __construct( \Yiisoft\Db\Query\QueryInterface $query, \Yiisoft\Data\Reader\Sort|null $sort = null, integer $offset = 0, integer|null $limit = null, string|null $countParam = null, \Yiisoft\Data\Reader\FilterInterface $filter = new All(), \Yiisoft\Data\Reader\FilterInterface $having = new All(), integer|null $batchSize = null, array $extraFilterHandlers = [], array|Yiisoft\Data\Db\FieldMapper\FieldMapperInterface $fieldMapper = [] ): mixed | ||
| $query | \Yiisoft\Db\Query\QueryInterface | |
| $sort | \Yiisoft\Data\Reader\Sort|null | |
| $offset | integer | |
| $limit | integer|null | |
| $countParam | string|null | |
| $filter | \Yiisoft\Data\Reader\FilterInterface | |
| $having | \Yiisoft\Data\Reader\FilterInterface | |
| $batchSize | integer|null | |
| $extraFilterHandlers | array | |
| $fieldMapper | array|Yiisoft\Data\Db\FieldMapper\FieldMapperInterface | |
public function __construct(
private readonly QueryInterface $query,
private ?Sort $sort = null,
private int $offset = 0,
private ?int $limit = null,
private ?string $countParam = null,
private FilterInterface $filter = new All(),
private FilterInterface $having = new All(),
private ?int $batchSize = null,
array $extraFilterHandlers = [],
array|FieldMapperInterface $fieldMapper = [],
) {
$this->fieldMapper = is_array($fieldMapper) ? new ArrayFieldMapper($fieldMapper) : $fieldMapper;
$this->filterHandler = new FilterHandler(
[
new AllHandler(),
new NoneHandler(),
new AndXHandler(),
new OrXHandler(),
new EqualsHandler(),
new GreaterThanHandler(),
new GreaterThanOrEqualHandler(),
new LessThanHandler(),
new LessThanOrEqualHandler(),
new LikeHandler(),
new InHandler(),
new ExistsHandler(),
new NotHandler(),
new BetweenHandler(),
new EqualsNullHandler(),
new EqualsExpressionHandler(),
...$extraFilterHandlers,
],
$this->fieldMapper,
);
}
| public count( ): integer |
final public function count(): int
{
if ($this->count === null) {
$q = $this->countParam ?? '*';
if ($q === '*' && is_array($this->cache) && $this->limit === null && $this->offset === 0) {
$this->count = count($this->cache);
} else {
$query = $this->getPreparedQuery();
$query->offset(null);
$query->limit(null);
$query->orderBy('');
$count = $query->count($q);
if (is_string($count)) {
throw new RuntimeException(
sprintf(
'Number of records is too large to fit into a PHP integer. Got %s.',
$count,
),
);
}
$this->count = $count;
}
}
return $this->count;
}
| public getFilter( ): \Yiisoft\Data\Reader\FilterInterface |
final public function getFilter(): FilterInterface
{
return $this->filter;
}
| public getIterator( ): Generator |
final public function getIterator(): Generator
{
if ($this->batchSize !== null) {
foreach ($this->getPreparedQuery()->batch($this->batchSize) as $data) {
/** @psalm-var array<TKey, TValue> $data */
yield from $data;
}
/** @infection-ignore-all */
return;
}
if (is_array($this->cache)) {
yield from $this->cache;
return;
}
/** @psalm-var array<TKey, TValue> */
$this->cache = $this->getPreparedQuery()->all();
yield from $this->cache;
}
| public getPreparedQuery( ): \Yiisoft\Db\Query\QueryInterface |
final public function getPreparedQuery(): QueryInterface
{
$query = (clone $this->query)
->andWhere(
$this->filterHandler->handle($this->filter),
)
->andHaving(
$this->filterHandler->handle($this->having),
);
if ($this->limit !== null) {
$query->limit($this->limit);
}
$query->offset($this->offset);
if ($this->sort !== null) {
$query->addOrderBy(
$this->convertSortToOrderBy($this->sort),
);
}
return $query;
}
| public getSort( ): \Yiisoft\Data\Reader\Sort|null |
final public function getSort(): ?Sort
{
return $this->sort;
}
| public readOne( ): array|object|null |
final public function readOne(): array|object|null
{
if (is_array($this->cache)) {
$key = array_key_first($this->cache);
return $key === null ? null : $this->cache[$key];
}
return $this->withLimit(1)->getIterator()->current();
}
| public withBatchSize( integer|null $batchSize ): Yiisoft\Data\Db\QueryDataReader | ||
| $batchSize | integer|null | |
| return | Yiisoft\Data\Db\QueryDataReader |
The new instance with the specified batch size. |
|---|---|---|
final public function withBatchSize(?int $batchSize): static
{
if ($batchSize !== null && $batchSize < 1) {
throw new InvalidArgumentException('$batchSize cannot be less than 1.');
}
$new = clone $this;
$new->batchSize = $batchSize;
return $new;
}
| public withCountParam( string|null $countParam ): Yiisoft\Data\Db\QueryDataReader | ||
| $countParam | string|null | |
| return | Yiisoft\Data\Db\QueryDataReader |
The new instance with the specified count parameter. |
|---|---|---|
final public function withCountParam(?string $countParam): static
{
if ($this->countParam === $countParam) {
return $this;
}
$new = clone $this;
$new->count = null;
$new->countParam = $countParam;
return $new;
}
| public withFilter( \Yiisoft\Data\Reader\FilterInterface $filter ): Yiisoft\Data\Db\QueryDataReader | ||
| $filter | \Yiisoft\Data\Reader\FilterInterface | |
| return | Yiisoft\Data\Db\QueryDataReader |
The new instance with the specified filter. |
|---|---|---|
final public function withFilter(FilterInterface $filter): static
{
$new = clone $this;
$new->filter = $filter;
$new->count = $new->cache = null;
return $new;
}
| public withHaving( \Yiisoft\Data\Reader\FilterInterface $having ): Yiisoft\Data\Db\QueryDataReader | ||
| $having | \Yiisoft\Data\Reader\FilterInterface | |
| return | Yiisoft\Data\Db\QueryDataReader |
The new instance with the specified having condition. |
|---|---|---|
final public function withHaving(FilterInterface $having): static
{
$new = clone $this;
$new->having = $having;
$new->count = $new->cache = null;
return $new;
}
| public withLimit( integer|null $limit ): Yiisoft\Data\Db\QueryDataReader | ||
| $limit | integer|null | |
| return | Yiisoft\Data\Db\QueryDataReader |
The new instance with the specified limit. |
|---|---|---|
final public function withLimit(?int $limit): static
{
if ($limit < 0) {
throw new InvalidArgumentException('$limit must not be less than 0.');
}
$new = clone $this;
$new->cache = null;
$new->limit = $limit;
return $new;
}
| public withOffset( integer $offset ): Yiisoft\Data\Db\QueryDataReader | ||
| $offset | integer | |
| return | Yiisoft\Data\Db\QueryDataReader |
The new instance with the specified offset. |
|---|---|---|
final public function withOffset(int $offset): static
{
$new = clone $this;
$new->cache = null;
$new->offset = $offset;
return $new;
}
| public withSort( \Yiisoft\Data\Reader\Sort|null $sort ): Yiisoft\Data\Db\QueryDataReader | ||
| $sort | \Yiisoft\Data\Reader\Sort|null | |
| return | Yiisoft\Data\Db\QueryDataReader |
The new instance with the specified sort. |
|---|---|---|
final public function withSort(?Sort $sort): static
{
$new = clone $this;
$new->cache = null;
$new->sort = $sort;
return $new;
}
Signup or Login in order to comment.