Integrating Doctrine with Laravel Mark Garratt - London Laravel Meetup - October 2015
Introduction PHP Developer DDD Student Architecture Enthusiast Metalhead Archer
This Talk What is Doctrine? Installing Doctrine in Laravel Replacing Eloquent Using Doctrine https://github.com/mgarratt/ laravel-doctrine-example
What is Doctrine? • Doctrine is an ORM based on the Data Mapper Pattern • Has it’s own underlying DBAL supporting MySQL, PostgreSQL and MSSQL • Uses a proprietary object oriented SQL dialect called Doctrine Query Language (DQL) • Store metadata as Annotations, XML, YAML or custom implementations • Uses Unit of Work Pattern to reduce database queries
How does it work? • Models are Plain Old PHP Objects called Entities • Entities are persisted through an EntityManager • Entities are retrieved through a Repository • Schema defined using Entity Metadata
LaravelDoctrine.org • Preconfigured metadata, connections and caching • Annotations, yaml, xml, config and static php meta data mappings • Pagination • Extendable: extend or add your own drivers for metadata, connections or cache • Change metadata, connection or cache settings easy with a resolved hook
LaravelDoctrine.org • Multiple entity managers and connections • Simple authentication implementation • Password reminders implementation • Doctrine console commands • DoctrineExtensions supported • Timestamps, Softdeletes and TablePrefix listeners
Replacing Eloquent with Doctrine • Remove extends model • Replace Authenticatable Trait and Contract • Include ORM Mapping and Timestamps • Add fields, getters, setters • Annotate everything
Replacing Eloquent with Doctrine • Update AuthController validation to use Doctrine unique syntax • Replace AuthController create with a version that uses EntityManager • Replace PasswordController resetPassword with a version that uses EntityManager
Where are we now? • Doctrine in use instead of Eloquent • Separated persistence from entities • Standard Authentication and Password Reset working • Replaced save calls with global EntityManager • Lost ability to query and save from entities • Entities must be persisted and flushed to get an ID • Also missing: Migrations & Seeds
Remove EntityManager Alias • Begin using the Repository Pattern • EntityManger injected to Repository • Repository deals with storing and retrieving entities only
Built in EntityRepository • Repositories extend EntityRepository • Constructed with EntityManager and ClassMetaData • Provides find() findAll() findBy() and generic matching() methods
FlushEntityManager Middleware • Calling persist on EntityManager adds those changes to the Unit of Work • flush() must be called to actually persist to the DB • Add a Middleware to do this at the end of every request
Application Generated IDs • Use ramsey/uuid library - RFC 4122 version 1, 3, 4, and 5 compliant • Create global uuid() function to return a UUID V4 • Use named constructors • Update metadata so ID is a string and Generator Strategy is NONE
Migrations • Provided by laravel-­‐doctrine/migrations • No need to write yourself
 php  artisan  doctrine:migrations:diff   • Update your Entities then re-run diff to create a new migration • Run migrations using
 php  artisan  doctrine:migrations:migrate
Seeds • Work exactly the same • Work directly with DBAL • Create Entities and persist • Model Factories do not work (they’re part of Eloquent)
Switch to YAML Mapping • Config doctrine.managers.default.path defines where YAML files are stored • Files named as fully qualified name with slashes replaced by dots: App.User.dcm.yml
 (dcm = Doctrine Mapping File) • Also need a mapping for LaravelDoctrineORM AuthPasswordsPasswordReminder
What has this achieved? Gone from an application that depends on a SQL database to an application that happens to be persisted on an SQL database • Doctrine replaced with Eloquent • Strong separation of concerns - Repositories deal with persistence, Entities deal with business logic • ID creation responsibility of the application • Huge amount of Laravel functionality preserved • Good foundation for DDD-style application
Moving Forward • Build up methods in repository • Take advantage of Laravel Doctrine pagination • Learn about Query Builder and DQL
Obligatory Questions / Contact Slide Any Questions? Mark Garratt
 Twitter: @MGarratt88
 GitHub: mgarratt https://joind.in/15859

Integrating Doctrine with Laravel

  • 1.
    Integrating Doctrine with Laravel MarkGarratt - London Laravel Meetup - October 2015
  • 2.
  • 3.
    This Talk What isDoctrine? Installing Doctrine in Laravel Replacing Eloquent Using Doctrine https://github.com/mgarratt/ laravel-doctrine-example
  • 4.
    What is Doctrine? •Doctrine is an ORM based on the Data Mapper Pattern • Has it’s own underlying DBAL supporting MySQL, PostgreSQL and MSSQL • Uses a proprietary object oriented SQL dialect called Doctrine Query Language (DQL) • Store metadata as Annotations, XML, YAML or custom implementations • Uses Unit of Work Pattern to reduce database queries
  • 5.
    How does itwork? • Models are Plain Old PHP Objects called Entities • Entities are persisted through an EntityManager • Entities are retrieved through a Repository • Schema defined using Entity Metadata
  • 6.
    LaravelDoctrine.org • Preconfigured metadata, connectionsand caching • Annotations, yaml, xml, config and static php meta data mappings • Pagination • Extendable: extend or add your own drivers for metadata, connections or cache • Change metadata, connection or cache settings easy with a resolved hook
  • 7.
    LaravelDoctrine.org • Multiple entitymanagers and connections • Simple authentication implementation • Password reminders implementation • Doctrine console commands • DoctrineExtensions supported • Timestamps, Softdeletes and TablePrefix listeners
  • 11.
    Replacing Eloquent with Doctrine •Remove extends model • Replace Authenticatable Trait and Contract • Include ORM Mapping and Timestamps • Add fields, getters, setters • Annotate everything
  • 12.
    Replacing Eloquent with Doctrine •Update AuthController validation to use Doctrine unique syntax • Replace AuthController create with a version that uses EntityManager • Replace PasswordController resetPassword with a version that uses EntityManager
  • 13.
    Where are wenow? • Doctrine in use instead of Eloquent • Separated persistence from entities • Standard Authentication and Password Reset working • Replaced save calls with global EntityManager • Lost ability to query and save from entities • Entities must be persisted and flushed to get an ID • Also missing: Migrations & Seeds
  • 14.
    Remove EntityManager Alias •Begin using the Repository Pattern • EntityManger injected to Repository • Repository deals with storing and retrieving entities only
  • 15.
    Built in EntityRepository •Repositories extend EntityRepository • Constructed with EntityManager and ClassMetaData • Provides find() findAll() findBy() and generic matching() methods
  • 16.
    FlushEntityManager Middleware • Calling persiston EntityManager adds those changes to the Unit of Work • flush() must be called to actually persist to the DB • Add a Middleware to do this at the end of every request
  • 17.
    Application Generated IDs •Use ramsey/uuid library - RFC 4122 version 1, 3, 4, and 5 compliant • Create global uuid() function to return a UUID V4 • Use named constructors • Update metadata so ID is a string and Generator Strategy is NONE
  • 18.
    Migrations • Provided bylaravel-­‐doctrine/migrations • No need to write yourself
 php  artisan  doctrine:migrations:diff   • Update your Entities then re-run diff to create a new migration • Run migrations using
 php  artisan  doctrine:migrations:migrate
  • 19.
    Seeds • Work exactlythe same • Work directly with DBAL • Create Entities and persist • Model Factories do not work (they’re part of Eloquent)
  • 20.
    Switch to YAMLMapping • Config doctrine.managers.default.path defines where YAML files are stored • Files named as fully qualified name with slashes replaced by dots: App.User.dcm.yml
 (dcm = Doctrine Mapping File) • Also need a mapping for LaravelDoctrineORM AuthPasswordsPasswordReminder
  • 22.
    What has thisachieved? Gone from an application that depends on a SQL database to an application that happens to be persisted on an SQL database • Doctrine replaced with Eloquent • Strong separation of concerns - Repositories deal with persistence, Entities deal with business logic • ID creation responsibility of the application • Huge amount of Laravel functionality preserved • Good foundation for DDD-style application
  • 23.
    Moving Forward • Buildup methods in repository • Take advantage of Laravel Doctrine pagination • Learn about Query Builder and DQL
  • 24.
    Obligatory Questions / ContactSlide Any Questions? Mark Garratt
 Twitter: @MGarratt88
 GitHub: mgarratt https://joind.in/15859