Skip to content

Statement

Raxos\Database\Query\Statement wraps a prepared PDOStatement. It binds parameters, executes the query and turns rows into arrays, stdClass values or hydrated models. A statement is normally created for you by Query, but you can build one directly with Connection::prepare() or Db::prepare().

php
class Statement implements StatementInterface

Binding

MethodDescription
bind(string $name, bool|string|int|float|null $value, ?int $type = null): staticBinds a parameter value, inferring the PDO type from the value when $type is omitted.

Fetching

MethodDescription
run(): intExecutes the statement and returns the affected row count.
single(int $fetchMode = PDO::FETCH_ASSOC): Model|stdClass|array|nullExecutes and fetches the first row, hydrating a model when one is assigned.
array(int $fetchMode = PDO::FETCH_ASSOC): arrayExecutes and fetches every row.
arrayList(int $fetchMode = PDO::FETCH_ASSOC): ArrayListInterface|ModelArrayListExecutes and returns the rows as a collection.
cursor(int $fetchMode = PDO::FETCH_ASSOC): GeneratorExecutes and yields rows one at a time.
fetchColumn(int $index = 0): mixedExecutes and returns a single column value from the first row.
rowCount(): intReturns the affected row count of the last execution.

Model binding

MethodDescription
withModel(string $class): staticHydrates fetched rows into instances of the given model.
withoutModel(): staticRemoves the model binding, so rows come back raw.
eagerLoad(array $relationships): voidSets the relations to eager load for hydrated models.
eagerLoadDisable(array $relationships): voidSets the relations to skip when eager loading.

Example

php
<?php
declare(strict_types=1);

use Raxos\Database\Db;

$statement = Db::prepare('select * from users where id = :id');
$statement->bind('id', 'usr_1');

$user = $statement->single();

When a statement is prepared from a model query, single(), array() and arrayList() return hydrated model instances and honor any eager loading configured on the query. See Query for how those are set up.