Documentation · Chapter 07

Make reading-list
truly persistent.

The workshop replaces sessions with SQLite while preserving routes, validation, security, and views already built.

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

Transformation plan

  1. Install Data with the sqlite driver.
  2. Generate Book as a persistent entity.
  3. Create and apply create_books_table.
  4. Add AppDbContext and inject it into BookController.
  5. Replace session reads and writes with DbSet.

Commands

phpaml — zsh
aml install data --driver sqlite
aml make:entity Book
aml make:migration create_books_table
aml data:migrate
aml data:status
aml test

What 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.