mini\Database\Model
abstract
class
Documentation
Abstract base class for database entities (Active Record pattern)
Provides save(), delete(), find(), and query() methods for entities. Table name is detected from #[Table] attribute via model(). Primary key is detected from #[PrimaryKey] attribute via model() (defaults to 'id').
Automatically handles:
- Dehydration (entity → array) via Dehydrator (includes #[CreatedAt]/#[UpdatedAt] timestamps)
- Validation via WriteValidator (if validation attributes are present)
- Identity tracking (correctly detects insert vs update even if PK changes)
- Authorization via can() system (override provideCanList/Create/Read/Update/Delete)
Safe vs Unsafe methods:
- saveUnsafe(), deleteUnsafe() are final — guaranteed persistence layer
- save(), delete() use two-layer auth: can() + updatable()/deletable() row scoping
- query(), find() are overridable — default pass-through to unsafe methods
- updatable(), deletable() — row-level write scoping (default to query())
- queryUnsafe(), findUnsafe() are final — raw database access
Override query() to filter rows by user permissions:
public static function query(): Query {
auth()->requireLogin();
return static::queryUnsafe()->eq('org_id', auth()->getClaim('org_id'));
}
Override provideCanUpdate/Delete etc. for action authorization:
public function provideCanUpdate(): ?bool {
return $this->user_id === auth()->getUserId();
}
#[Table('users')]
class User extends Model
{
#[PrimaryKey]
public ?int $id = null;
public string $email = '';
public string $name = '';
}
Properties (3)
mixed $_modelOriginalId
Tracks the original primary key value from when entity was loaded.
bool $_modelDeleted
Whether this entity has been deleted from the database.
array $_modelOriginalData
Snapshot of property values from when the entity was loaded or last saved.
Methods (22)
Get the database connection
Get the original primary key from when entity was loaded.
Can the current user list entities of this class?
Can the current user create new entities of this class?
Can the current user read this entity?
Can the current user update this entity?
Can the current user delete this entity?
Create a query builder without authorization filtering
Find an entity by primary key without authorization filtering
Save entity without authorization check — the guaranteed persistence layer
Delete entity without authorization check — the guaranteed persistence layer
Create a query builder with authorization filtering
Find an entity by primary key with authorization filtering
Row-level scope for updates — which rows can be updated?
Row-level scope for deletes — which rows can be deleted?
Save entity with authorization
Delete entity with authorization
Check which properties have changed since the entity was loaded (or last saved).
Check if entity is valid without saving
Reset identity on clone — cloned entity is treated as new (unsaved)
Mark this entity as loaded from the database
Snapshot all public property values for dirty tracking