Book Image

Domain-Driven Design in PHP

By : Keyvan Akbary, Carlos Buenosvinos, Christian Soronellas
Book Image

Domain-Driven Design in PHP

By: Keyvan Akbary, Carlos Buenosvinos, Christian Soronellas

Overview of this book

Domain-Driven Design (DDD) has arrived in the PHP community, but for all the talk, there is very little real code. Without being in a training session and with no PHP real examples, learning DDD can be challenging. This book changes all that. It details how to implement tactical DDD patterns and gives full examples of topics such as integrating Bounded Contexts with REST, and DDD messaging strategies. In this book, the authors show you, with tons of details and examples, how to properly design Entities, Value Objects, Services, Domain Events, Aggregates, Factories, Repositories, Services, and Application Services with PHP. They show how to apply Hexagonal Architecture within your application whether you use an open source framework or your own.
Table of Contents (24 chapters)
Title Page
Credits
Foreword
About the Authors
Acknowledgments
www.PacktPub.com
Customer Feedback
Dedication
Preface
14
Bibliography
15
The End

Persisting Entities


Currently, as discussed earlier in the chapter, the best tool for saving Entity state to a persistent store is Doctrine ORM. Doctrine has several ways to specify Entity metadata: by annotations in Entity code, by XML, by YAML, or by plain PHP. In this chapter, we'll discuss in depth why annotations are not the best thing to use when mapping Entities.

Setting Up Doctrine

First of all, we need to require Doctrine through Composer. At the root folder of the project, the command below has to be executed:

 > php composer.phar require "doctrine/orm=^2.5"

Then, these lines will allow you to set up Doctrine:

require_once '/path/to/vendor/autoload.php';

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = ['/path/to/entity-files'];
$isDevMode = false;

// the connection configuration
$dbParams = [
    'driver'   => 'pdo_mysql',
    'user'     => 'the_database_username',
    'password' => 'the_database_password',
    'dbname'   => 'the_database_name...