RowQuery

Class.

use Saij\WpAppFramework\Table\RowQuery;

A query for rows of a table, started with LogEntry::query() and returning row objects; it writes its SQL itself.

A query never changes: every method returns a changed copy, so a query can be passed on and varied safely. Keep the result – $query->limit(3); alone does nothing, and PHP warns about it (#[\NoDiscard]).

$rows = LogEntry::query()
    ->where(LogEntryFields::level()->equals(Level::Error))
    ->orderBy(LogEntryFields::created(), Direction::Desc)
    ->limit(20)
    ->get();

Defaults: all rows, in the order of the primary key. Columns are compared as stored: strings without regard to case (the collation of WordPress), NULL like PHP compares null – notEquals() and notIn() of a nullable column also match rows without value. NULL sorts first in ascending order (MariaDB).

Methods

count()

count(): int

The number of all matching rows; limit() and offset() do not count.

Returns int

delete()

delete(): int

Deletes the matching rows (with limit(), the first ones in the order) and returns how many. Row objects loaded before keep their primary key.

Returns int

first()

first(): ?AbstractRow

The first matching row, null without one.

Returns T|null

get()

get(): array

The matching rows.

Returns list<T>

limit()

limit(int $limit): static

At most this many rows.

ParameterTypeDefaultDescription
limitintrequiredThe maximum number of rows, at least 1; a smaller number throws an InvalidArgumentException.

Returns RowQuery

offset()

offset(int $offset): static

Skips this many rows (with limit()).

ParameterTypeDefaultDescription
offsetintrequiredThe number of rows to skip, at least 0; a negative number throws an InvalidArgumentException. Takes effect only together with limit(); delete() does not allow it.

Returns RowQuery

orderBy()

orderBy(Field $field, Direction $direction = Direction::Asc): static

Sorts by a column; further calls sort rows that are equal so far. Without orderBy(), rows come in the order of their primary key.

ParameterTypeDefaultDescription
fieldFieldrequiredA field of this table, from its class with #[TableFields]; a field of another table throws an InvalidArgumentException.
directionDirectionDirection::AscAscending (default) or descending.

Returns RowQuery

where()

where(Condition ...$conditions): static

Rows that meet all conditions (columns, Any, All); adds to the conditions of earlier calls.

ParameterTypeDefaultDescription
...conditionsConditionConditions created by the field classes of this table (a class with #[TableFields]), or groups of them with Any and All. Throws an InvalidArgumentException for conditions on meta fields or terms, or on a column of another table.

Returns RowQuery