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.

 

Environment-driven CDN host for SonataMediaBundle images in wkhtmltopdf generated with Symfony KnpSnappyBundle and TWIG

To display images in wkhtmltopdf you need to enable images:

 

In addition you have to use absolute paths in your twig template. But sometimes you want to use different path based on your environment. Using assets you can easily do this:

 

Instead of setting the CDN host statically you can use parameters.yml:

Source: https://github.com/Jolg42/sonata-sandbox/blob/master/app/config/parameters.yml.sample#L30

 

Then in your sonata_media.yml :

Source: https://github.com/Jolg42/sonata-sandbox/blob/master/app/config/sonata/sonata_media.yml#L33

 

Now you can use the Sonata Media helpers to display the images:

 

Using PHP YAML Extension and Unicode on Entity column with Doctrine Events

I have a Rails legacy database that uses a YAML formatted array in a column. My new App is based on Symfony2 and Doctrine2.

To make this column useable in Sonata Admin I had to (re-)convert between PHP Array and YAML.

 

This worked fine using Doctrine Events:

 

Note the YAML_UTF8_ENCODING constant when emitting. By default yaml_emit uses the YAML_ANY_ENCODING constant.

All constants can be found here:

http://php.net/manual/en/yaml.constants.php

How to inject Symfony Security into Doctrine Event Listener without Circular Reference Exception

As of Symfony 2.6, you can inject the security.token_storage into your listener. This service will contain the token as used by the SecurityContext in <=2.5.

 

 

Backlinks:

How to use CHAR instead of VARCHAR with Doctrine ORM

 

How to check if Subject is New resp. action is create or edit in Sonata Admin

In your Admin Class:

In your Twig Template:

Source: https://github.com/sonata-project/SonataAdminBundle/blob/master/Resources/views/CRUD/base_edit_form.html.twig#L4

 

Backlinks:

Override list view twig template in SonataAdminBundle

There are two ways to override the list view template.

1. Admin Class

2. Service Setter Injection

As described in the docs.

 

Then take the original twig template and change the desired block e.g. list_header:

 

That’s it!

 

Backlinks:

 

Translating dynamic vars concatening object property in twig template using SonataAdminBundle

My Contract Entity has a history of states and the current state is displayed in the listView. The state is stored as a simple string e.g. “pending” in the database.

 

But the translation string are stored with a prefix e.g. “contracts.states.pending”:

 

To make the translation work inside the twig template you can concat the prefix and the property from the database like this:

 

Backlinks: