Transformation plan
- Install Data with the sqlite driver.
- Generate Book as a persistent entity.
- Create and apply create_books_table.
- Add AppDbContext and inject it into BookController.
- Replace session reads and writes with DbSet.
Commands
aml install data --driver sqlite
aml make:entity Book
aml make:migration create_books_table
aml data:migrate
aml data:status
aml testWhat changes in the controller
BEFORE — SESSION
$books = $this->session->get('books', []);
AFTER — DATA
$books = $this->db->books()->orderBy('title')->all();
The view still receives books. The storage change stays in the data-responsible layer; HTML does not need to know SQLite.
Verification plan
Create then reloadbook remains visible
New browser sessionsame catalog
Missing identifier404
Migration rerunno duplicate
Invalid titleno row created
Limits to understand
SQLite fits this workshop well. Heavy concurrent writes or several servers may justify MySQL, MariaDB, or PostgreSQL. Choose an engine from measured needs, not fashion.
SUMMARY
What to remember
- An entity represents a row as a PHP object.
- A migration versions database structure.
- DbSet provides reads, queries, adds, updates, and removals.
- DbContext can track changes and save them transactionally.
- Views stay independent from the storage engine.