Models in Tempest are objects that represent the data and functionality of your application. Thanks to Tempest's map
function, models can be as simple as an object, and their data could be coming from anywhere.
final class Book { #[Length(min: 1, max: 120)] public string $title; public ?Author $author = null; /** @var \App\Modules\Books\Models\Chapter[] */ public array $chapters = []; }
Since most models are persisted within a database though, Tempest provides a trait called BaseModel
(note: this trait will probably be renamed soon). Using this trait will make it so that your model classes can be persisted from and to a database.
Such model classes look like this:
final class Book implements Model { use BaseModel; #[Length(min: 1, max: 120)] public string $title; public ?Author $author = null; /** @var \App\Modules\Books\Models\Chapter[] */ public array $chapters = []; }
Some key things to note about model definition:
Author
model.#[Length]
attribute. There'll be a dedicated chapter about validation in the future.Model
interface defines a set of methods that makes it easy to retrieve and persist models. It's important to note that the model class itself isn't concerned with SQL-specific query builders.