Skip to content

Db

Raxos\Database\Db is the static facade for the connection registry. It stores connections by id and delegates common operations (queries, execution, transactions, quoting and schema checks) to the resolved connection.

php
class Db

Resolving connections

MethodDescription
static get(?string $id = null): ?ConnectionInterfaceReturns the connection for the id, or the default connection, connecting it on first use. Returns null when none is registered.
static getOrFail(?string $id = null): ConnectionInterfaceSame as get(), but throws InvalidConnectionException when no connection is registered.
static register(ConnectionInterface $connection, ?string $id = null): voidRegisters a connection under the id, or as the default connection.
static unregister(string $id): voidRemoves a registered connection.

Delegated operations

Each of these resolves the connection (defaulting to the registered default) and forwards the call.

MethodDescription
static query(bool $prepared = true, ?string $id = null): QueryInterfaceStarts a new query on the resolved connection.
static prepare(QueryInterface|string $query, array $options = [], ?string $id = null): StatementInterfacePrepares a statement.
static execute(QueryInterface|string $query, ?string $id = null): intExecutes a statement and returns the affected row count.
static column(QueryInterface|string $query, ?string $id = null): string|intExecutes and returns the first column of the first row.
static quote(BackedEnum|string|int|float|bool $value, int $type = PDO::PARAM_STR, ?string $id = null): stringQuotes a value for a raw query.
static lastInsertId(?string $name = null, ?string $id = null): stringReturns the last insert id as a string.
static lastInsertIdInteger(?string $name = null, ?string $id = null): intReturns the last insert id as an int.

Transactions

MethodDescription
static transaction(?string $id = null): boolBegins a transaction, or a savepoint when already inside one.
static commit(?string $id = null): boolCommits the current transaction or savepoint.
static rollBack(?string $id = null): boolRolls back the current transaction or savepoint.
static inTransaction(?string $id = null): boolReturns true when a transaction is active.

Schema introspection

MethodDescription
static tableExists(string $table, ?string $id = null): boolReturns true if the table exists.
static tableColumns(string $table, ?string $id = null): arrayReturns the column names of a table.
static tableColumnExists(string $table, string $column, ?string $id = null): boolReturns true if the column exists in the table.

Example

php
<?php
declare(strict_types=1);

use Raxos\Database\Connection\MySql;
use Raxos\Database\Db;

Db::register(MySql::createFromOptions(
    host: 'localhost',
    database: 'app',
    username: 'root',
));

Db::transaction();

try {
    Db::query()
        ->update('users', ['is_active' => 1])
        ->where('id', 'usr_1')
        ->run();

    Db::commit();
} catch (Throwable $err) {
    Db::rollBack();

    throw $err;
}

See connections for how connections are created and registered.