Skip to content

ORM attributes

The attributes in Raxos\Database\Orm\Attribute describe how a model maps to a table: its columns and keys, its relations, value conversion, embeddables and single table inheritance. A class attribute targets the model class, a property attribute targets a mapped property.

Structure

AttributeTargetPurpose
#[Table(string $name)]classDeclares the database table for a model.
#[Column(?string $key = null)]propertyMarks a property as a mapped column, optionally with a different column key.
#[PrimaryKey]propertyMarks a column property as (part of) the primary key. Extends #[Column].
#[ForeignKey]propertyMarks a column property as a foreign key. Extends #[Column].
#[Alias(?string $alias = null)]propertyGives a property an export alias used by toArray() and jsonSerialize().

Visibility and mutability

AttributeTargetPurpose
#[Hidden]propertyHides a column, macro or relation from export by default.
#[Visible(array|string|null $only = null)]propertyForces a field visible by default, optionally restricted to a set of nested keys.
#[Immutable]propertyBlocks write access to the field after the model is hydrated.
#[Computed]propertyMarks a column as database computed, so it is excluded from insert and update.

Behavior

AttributeTargetPurpose
#[SoftDelete(string $column = 'deleted_on')]classEnables soft deletes using the given timestamp column.
#[OnDuplicateUpdate(array|string $fields)]classLists fields to update automatically on an insert that hits a duplicate key.
#[ConnectionId(string $connectionId = 'default')]classSelects which connection, as registered with Db::register(), the model uses, so a table's model can target a non-default registered connection.
#[Macro(Closure $callback, bool $isCached = true)]propertyDefines a computed, by default cached, virtual property.

Value conversion and embeddables

AttributeTargetPurpose
#[Caster(string $casterClass)]propertyAssigns a caster that converts a column value between database and PHP form.
#[Embeddable]classMarks a value object class as embeddable inside a model's table.
#[Embedded(string $prefix = '')]propertyMounts an embeddable on a model property, with an optional column prefix.
#[Polymorphic(string $column = 'type', array $map = [])]classMaps a discriminator column to concrete model subclasses that share one table.

Relations

All relation attributes accept key overrides (referenceKey, declaringKey and their table variants), an eagerLoad flag and a withDeleted flag. The ones that map to a collection also take an orderBy.

AttributeTargetFirst arguments
#[HasOne]propertyKey overrides only; the related model is inferred from the property type.
#[HasMany(string $referenceModel, ...)]propertyThe related model class.
#[BelongsTo]propertyKey overrides only; the related model is inferred from the property type.
#[BelongsToMany(string $referenceModel, ?string $linkingTable = null, ...)]propertyThe related model and optional linking table.
#[HasOneThrough(string $linkingModel, ...)]propertyThe intermediate model.
#[HasManyThrough(string $referenceModel, string $linkingModel, ...)]propertyThe related model and the intermediate model.
#[BelongsToThrough(string $linkingModel, ...)]propertyThe intermediate model.

Example

php
<?php
declare(strict_types=1);

use Raxos\Database\Orm\{Model, ModelArrayList};
use Raxos\Database\Orm\Attribute\{
    Alias, BelongsTo, Caster, Column, HasMany, Hidden, PrimaryKey, SoftDelete, Table
};
use Raxos\Database\Orm\Caster\BooleanCaster;

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

    #[Column]
    public string $name;

    #[Hidden]
    #[Column]
    public string $passwordHash;

    #[Caster(BooleanCaster::class)]
    #[Column]
    public bool $isActive;

    #[Alias('created_at')]
    #[Column]
    public int $createdOn;

    #[HasMany(Post::class)]
    public ModelArrayList $posts;
}

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

    #[Column]
    public string $userId;

    #[BelongsTo]
    public User $author;
}

For the concepts behind these attributes, see models, relations and casters, embeddables and polymorphic models.