Documentation · Chapter 07

Give books
a typed shape.

An entity describes in PHP the values composing a record and the table it belongs to.

We replace reading-list's temporary storage with SQLite without changing the application's purpose.

Generate then complete Book

phpaml — zsh
aml make:entity Book

src/models/Book.php

phpaml — zsh
<?php

declare(strict_types=1);

namespace App\Models;

use AML\Data\Entity;
use AML\Data\Metadata\{Key, Table};

#[Table('books')]
final class Book extends Entity
{
    #[Key]
    public int $id;
    public string $title;
    public string $author;
}

Read every element

#[Table('books')]maps the class to the books table
extends Entitymakes the class persistable by Data
#[Key]marks the primary key
public string $titledescribes a required text value

Why does the identifier have no value?

Before insertion, the book does not exist in the table. The database generates its key; DbSet::add() then writes it back to $book->id. The same PHP object now knows its persisted identity.

An entity is not a table

The class describes the PHP-side shape. The migration actually creates database structure. Both must stay coherent.