This document summarizes a presentation about integrating the Doctrine ORM with Laravel. It discusses what Doctrine is, how to install and configure it with Laravel, replacing Eloquent with Doctrine, using repositories, migrations, seeds, and other features. The presentation shows how to transition an application from using Eloquent to Doctrine to achieve a stronger separation of concerns and set up a foundation for domain-driven design principles.
3. This Talk
What is Doctrine?
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 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
6. 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
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 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
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 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
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 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
19. Seeds
• Work exactly the same
• Work directly with DBAL
• Create Entities and persist
• Model Factories do not work
(they’re part of Eloquent)
20. 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
21.
22. 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
23. Moving Forward
• Build up methods in repository
• Take advantage of Laravel
Doctrine pagination
• Learn about Query Builder
and DQL
24. Obligatory Questions /
Contact Slide
Any Questions?
Mark Garratt
Twitter: @MGarratt88
GitHub: mgarratt
https://joind.in/15859