Skip to content
Laureano F. Lamonega edited this page Jul 17, 2026 · 4 revisions

Model

The base model provides CRUD operations and automatic table name guessing.

Defining a Model

use Antimonial\Model\Model;

class User extends Model {}

Table Name Convention

PascalCase class names are converted to snake_case plural:

Class Table Name
User users
BlogPost blog_posts
Category categorys (naive — set $table explicitly for irregular plurals)

To use a custom table name, override the $table property:

class Category extends Model
{
    protected string $table = 'categories';
}

Primary Key

The default primary key is id. Override with:

class User extends Model
{
    protected string $primaryKey = 'user_id';
}

Timestamps

Enable automatic created_at / updated_at:

class User extends Model
{
    protected bool $timestamps = true;
}

CRUD Operations

Find

$user = (new User())->find(42);  // object or null

All

$users = (new User())->all();  // array of objects

Where

$users = (new User())->where('active', true)->get();

Insert

$id = (new User())->insert([
    'name'  => 'John',
    'email' => 'john@example.com',
]);

Update

(new User())->update(42, ['name' => 'Jane']);

Delete

(new User())->delete(42);

Query Builder Access

$qb = (new User())->query();  // Get the underlying QueryBuilder
$user = (new User())->query()->where('email', 'john@example.com')->first();

Clone this wiki locally