Persistence Patterns

Persistence patterns decide whether domain objects save themselves, whether mapping is separate, and how related changes are coordinated.

Why This Matters

Active Record can be productive for record-centered applications; Data Mapper separates persistence from domain objects; Unit of Work tracks coordinated changes; repositories express application queries.

Working Model

Eloquent commonly presents an Active Record style. Doctrine ORM uses mapped entities, an EntityManager, identity map, and Unit of Work. A Table Data Gateway represents table operations without making rows responsible for persistence.

Practical Rules

  • Choose by domain complexity and framework conventions.
  • Keep transaction ownership at the use-case boundary.
  • Avoid generic repositories that hide useful queries.
  • Know when SQL executes.
  • Use query builders or SQL for bulk operations.

Failure Modes

  • Mixing several patterns without ownership.
  • Putting remote calls in entity save hooks.
  • Treating repositories as universal CRUD wrappers.
  • Assuming an ORM removes N+1 or transaction concerns.

Verification

  • Trace one write from use case to SQL.
  • Count queries.
  • Test transaction rollback.
  • Compare bulk and object-oriented paths.

What You Should Be Able To Do

After this lesson, you should be able to explain Active Record, Data Mapper, Table Data Gateway, Repository, Identity Map, and Unit of Work tradeoffs, choose a suitable approach for a real PHP project, and verify the result instead of relying on assumptions.

Practice

Practice: Choose A Persistence Pattern

Choose a persistence style for a simple admin CRUD tool and a complex billing domain.

Your answer must:

  • state the intended outcome;
  • show the commands, data flow, or implementation shape;
  • identify at least one unsafe alternative;
  • explain how the result will be verified.
Show solution

An Active Record framework can suit straightforward CRUD. A richer billing domain may benefit from persistence-independent entities, Data Mapper, repositories, and explicit Unit of Work boundaries.

The important part is not memorising one command or vendor screen. The solution makes the invariant, failure behavior, and verification evidence explicit.

Practice: Locate Transaction Ownership

A service updates an order and payment record through repositories.

Your answer must:

  • state the intended outcome;
  • show the commands, data flow, or implementation shape;
  • identify at least one unsafe alternative;
  • explain how the result will be verified.
Show solution

The application use case should own the transaction because it knows both changes form one business operation. Repositories should not independently commit.

The important part is not memorising one command or vendor screen. The solution makes the invariant, failure behavior, and verification evidence explicit.

Practice: Review Hidden Queries

An entity serializer triggers relationship loads.

Your answer must:

  • state the intended outcome;
  • show the commands, data flow, or implementation shape;
  • identify at least one unsafe alternative;
  • explain how the result will be verified.
Show solution

Identify lazy loads, replace them with explicit query shapes or projections, and test query counts. Persistence convenience must not make response cost invisible.

The important part is not memorising one command or vendor screen. The solution makes the invariant, failure behavior, and verification evidence explicit.