ORM
PointArt's ORM maps PHP classes to database tables using attributes. Extend Model, annotate your class, and get find(), save(), and delete() with no SQL. For more complex queries — dynamic finders, custom SQL, and count/exists operations — see Repositories.
Namespaces
PointStart\ORM — ModelPointStart\Attributes — Entity, Column, Id
Defining a Model
Place model classes in app/models/. Annotate with #[Entity], mark columns with #[Column], and the primary key with #[Id]:
#[Entity('users')]
class User extends Model {
#[Id]
public ?int $id = null;
#[Column('name', 'varchar')]
public string $name;
#[Column('email', 'varchar', nullable: true)]
public ?string $email = null;
}
#[Entity]
| Parameter | Type | Required | Description |
|---|---|---|---|
tableName | string | Yes | The database table this class maps to |
#[Id]
No parameters. Marks the primary key property — must be ?int, initialised to null. The value is set automatically after save() on insert.
#[Column]
| Parameter | Type | Required | Description |
|---|---|---|---|
columnName | string | Yes | The database column name |
type | string | Yes | Column type hint (e.g. 'varchar', 'int', 'real') |
nullable | bool | No | Whether the column accepts NULL. Default: false |
Static Query Methods
| Method | SQL Equivalent |
|---|---|
User::find($id) | SELECT * WHERE pk = ? LIMIT 1 |
User::findAll() | SELECT * |
User::findBy(['col' => $val], $order, $limit) | SELECT * WHERE col = ? [ORDER/LIMIT] |
User::findOne(['col' => $val]) | SELECT * WHERE col = ? LIMIT 1 |
Instance Methods
// Create and save
$user = new User();
$user->name = 'Alice';
$user->email = '[email protected]';
$user->save(); // INSERT (id is null) → id set from lastInsertId
// Update
$user->name = 'Alice B.';
$user->save(); // UPDATE (id is not null)
// Delete
$user->delete(); // DELETE WHERE id = ?
| Method | Behaviour |
|---|---|
$entity->save() | INSERT if primary key is null, UPDATE otherwise |
$entity->delete() | DELETE WHERE pk = ? |
Database Connection
The PDO connection is created lazily on the first query. The driver is selected from config.php:
| Driver | DSN Format |
|---|---|
sqlite | sqlite:/path/to/database.sqlite |
mysql | mysql:host=…;port=…;dbname=…;charset=… |
pgsql | pgsql:host=…;port=…;dbname=… |
Testing
For unit tests, set
Model::$pdo directly to inject an in-memory SQLite instance:
Model::$pdo = new PDO('sqlite::memory:');
Next: Repositories →