Skip to content

Model

Raxos\Database\Orm\Model is the abstract base class for ORM models. It is backed by a Backbone that tracks the column data, exposes array and property style access through the raxos/foundation access traits, and provides array and JSON export with visibility rules. The static finder methods come from the Queryable trait it mixes in.

php
abstract class Model implements
    AccessInterface,
    ArrayableInterface,
    JsonSerializable,
    QueryableInterface,
    Stringable

Instance methods

MethodDescription
save(): voidInserts or updates the record for this instance.
destroy(): voidDeletes the record for this instance.
toArray(): arrayConverts the model to an array, honoring #[Hidden], #[Visible] and any per instance overrides.
jsonSerialize(): arrayReuses toArray() for json_encode().
makeHidden(array|string $keys): staticReturns a clone with the given keys hidden from export.
makeVisible(array|string $keys): staticReturns a clone with the given keys forced visible in export.
only(array|string $keys): staticReturns a clone that exports only the given keys.

Static query starters (Queryable)

MethodDescription
static query(bool $prepared = true): QueryInterfaceStarts a new query bound to the model.
static select(Select|QueryValueInterface|Stringable|array|string|int $keys = []): QueryInterfaceStarts a select query bound to the model.
static selectDistinct(...): QueryInterfaceStarts a select distinct query.
static where(...): QueryInterfaceShortcut for select()->where(...).
static having(...): QueryInterfaceShortcut for select()->having(...).
static table(): stringReturns the model's table name.
static col(string $key): ColumnLiteralReturns the fully qualified column literal for a key.

Static finders (Queryable)

MethodDescription
static all(int $offset = 0, int $limit = 20): ArrayListInterfaceReturns a page of all records.
static single(array|string|int $primaryKey): ?staticFinds one record by primary key, using the identity cache when possible.
static singleOrFail(array|string|int $primaryKey): staticSame as single(), but throws NotFoundException when nothing is found.
static find(array $primaryKeys): ModelArrayListFinds several records by primary key, using the cache where possible.
static exists(array|string|int $primaryKey): boolReturns true if a record with the given primary key exists.
static delete(array|string|int $primaryKey): voidDeletes a record by primary key without loading it.
static update(array|string|int $primaryKey, array $values): voidUpdates a record by primary key with the given column values.

Relation queries

Calling a relation name as a method returns its query, so you can refine it before executing:

php
$posts = $user->posts()
    ->where('is_published', 1)
    ->arrayList();

Example

php
<?php
declare(strict_types=1);

use Raxos\Database\Orm\Model;
use Raxos\Database\Orm\Attribute\{Column, PrimaryKey, Table};

#[Table('users')]
final class User extends Model
{
    #[PrimaryKey]
    #[Column]
    public string $id;

    #[Column]
    public string $name;
}

$user = User::singleOrFail('usr_1');
$user->name = 'Bas';
$user->save();

$active = User::where('is_active', 1)->arrayList();

Complete Queryable static methods

Beyond the starters and finders above, the Queryable trait exposes the full where and having shortcut families, the extra select variants and the aliased column helper. Each where/having shortcut starts a select() and applies the condition. See querying models for the narrative version.

MethodDescription
static whereIn(ColumnLiteral $column, ArrayableInterface|array $options): QueryInterfaceShortcut for select()->whereIn(...).
static whereNotIn(ColumnLiteral $column, ArrayableInterface|array $options): QueryInterfaceShortcut for select()->whereNotIn(...).
static whereNull(ColumnLiteral $column): QueryInterfaceShortcut for select()->whereNull(...).
static whereNotNull(ColumnLiteral $column): QueryInterfaceShortcut for select()->whereNotNull(...).
static whereExists(QueryInterface $query): QueryInterfaceShortcut for select()->whereExists(...).
static whereNotExists(QueryInterface $query): QueryInterfaceShortcut for select()->whereNotExists(...).
static having(...): QueryInterfaceShortcut for select()->having(...).
static havingIn(ColumnLiteral $column, ArrayableInterface|array $options): QueryInterfaceShortcut for select()->havingIn(...).
static havingNotIn(ColumnLiteral $column, ArrayableInterface|array $options): QueryInterfaceShortcut for select()->havingNotIn(...).
static havingNull(ColumnLiteral $column): QueryInterfaceShortcut for select()->havingNull(...).
static havingNotNull(ColumnLiteral $column): QueryInterfaceShortcut for select()->havingNotNull(...).
static havingExists(QueryInterface $query): QueryInterfaceShortcut for select()->havingExists(...).
static havingNotExists(QueryInterface $query): QueryInterfaceShortcut for select()->havingNotExists(...).
static selectFoundRows(Select|QueryValueInterface|Stringable|array|string|int $keys = [], bool $prepared = true): QueryInterfaceStarts a select that adds SQL_CALC_FOUND_ROWS.
static selectSuffix(string $suffix, Select|QueryValueInterface|Stringable|array|string|int $keys = [], bool $prepared = true): QueryInterfaceStarts a select with a raw suffix after the select keyword.
static alias(string $key, string $table): ColumnLiteralReturns the fully qualified column literal for a key against an aliased table.

Queryable hooks

Through QueryableInterface, a model implements two overridable static hooks. Both are no-ops on Model by default; override them to add computed columns and their supporting joins to every query for the model.

MethodDescription
static getQueryableColumns(Select $select): SelectAdds extra columns to every model query.
static getQueryableJoins(QueryInterface $query): QueryInterfaceAdds the joins those columns rely on.

See models for a worked example that combines both hooks with a #[Computed] property.

See models for the attribute driven definition, relations for links between models, and casters, embeddables and polymorphic models for value conversion.