Domain-Driven Design Repositories with Doctrine ORM and ODM in Symfony

Welcome to my first Domain-Driven Design in PHP post.

DDD in PHP

The following code is inspired by the Domain-Driven Design in PHP book written by Carlos Buenosvinos (@carlosbuenosvinos) and Keyvan Akbary (@keyvanakbary):

Value Objects

My examples code include Value Objects for the identifiers of Aggregate Roots (AR) For now they will be created manually. Later we will see how to use custom Doctrine DBAL types to persist them.

Interface

Doctrine Repository

ORM

ODM

The ODM implementation (e.g. MongoDB) looks very similar:

Service

Mapping

I recommend to use XML mapping for your Domain Models. This way your class remains POPO and does not mix ”’infrastructure”’ with ”’domain”’ concerns.

Feel free to join the discussion about this topic on GitHub:

Document

Here is an ODM Document example:

Entity

For those who still like to use annotations here is an example Entity:

Usage

Handler

Controller

Naming Conventions

Feel free to join the discussion about naming repositories and other best practices on GitHub.