Documentation · Chapter 13

Connect and query without hiding SQL realities.

Named connections separate responsibilities. DbSet builds validated, immutable queries while prepared values keep user data out of SQL structure.

Every lesson starts from the problem, explains the mechanism, then gives a concrete verification.

Name connections by responsibility

main can serve application writes while reporting serves analytics. A named connection makes the choice visible and prevents a feature from silently using the wrong database.

phpaml — zsh
aml install data --driver sqlite

$active = $context->set(User::class)
    ->where('active', '=', true)
    ->orderBy('name')
    ->paginate(1, 20);

Compose queries with DbSet

where, orderBy, whereIn, with, limit, find, first, count, and paginate return predictable results. Query objects are cloned as they change, so a base query can be reused safely.

Validate entities before persistence

Required, Email, and Length attributes reject invalid entities before persistence. Database constraints remain necessary because they protect every writer, not only this application.