SlideShare a Scribd company logo
1 of 24
Download to read offline
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

More Related Content

What's hot

Behavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using CucumberBehavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using Cucumber
KMS Technology
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
zand3rs
 

What's hot (20)

RESTful API - Best Practices
RESTful API - Best PracticesRESTful API - Best Practices
RESTful API - Best Practices
 
Let's start GraphQL: structure, behavior, and architecture
Let's start GraphQL: structure, behavior, and architectureLet's start GraphQL: structure, behavior, and architecture
Let's start GraphQL: structure, behavior, and architecture
 
PWA 與 Service Worker
PWA 與 Service WorkerPWA 與 Service Worker
PWA 與 Service Worker
 
High Performance Solr
High Performance SolrHigh Performance Solr
High Performance Solr
 
Developing Faster with Swagger
Developing Faster with SwaggerDeveloping Faster with Swagger
Developing Faster with Swagger
 
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
 
Behavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using CucumberBehavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using Cucumber
 
REST vs GraphQL
REST vs GraphQLREST vs GraphQL
REST vs GraphQL
 
Integrando sua App ao Mundo via REST/JSON
Integrando sua App ao Mundo via REST/JSONIntegrando sua App ao Mundo via REST/JSON
Integrando sua App ao Mundo via REST/JSON
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
Cucumber presenation
Cucumber presenationCucumber presenation
Cucumber presenation
 
Rest api and-crud-api
Rest api and-crud-apiRest api and-crud-api
Rest api and-crud-api
 
Conhecendo Apache Kafka
Conhecendo Apache KafkaConhecendo Apache Kafka
Conhecendo Apache Kafka
 
Object Oriented Javascript
Object Oriented JavascriptObject Oriented Javascript
Object Oriented Javascript
 
BDD WITH CUCUMBER AND JAVA
BDD WITH CUCUMBER AND JAVABDD WITH CUCUMBER AND JAVA
BDD WITH CUCUMBER AND JAVA
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.
 
Web API Basics
Web API BasicsWeb API Basics
Web API Basics
 
Node js (runtime environment + js library) platform
Node js (runtime environment + js library) platformNode js (runtime environment + js library) platform
Node js (runtime environment + js library) platform
 
Jquery Giriş
Jquery GirişJquery Giriş
Jquery Giriş
 
Cucumber presentation
Cucumber presentationCucumber presentation
Cucumber presentation
 

Similar to Integrating Doctrine with Laravel

Hibernate in XPages
Hibernate in XPagesHibernate in XPages
Hibernate in XPages
Toby Samples
 
Solr Recipes Workshop
Solr Recipes WorkshopSolr Recipes Workshop
Solr Recipes Workshop
Erik Hatcher
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
Terry Yoast
 
Modules Building Presentation
Modules Building PresentationModules Building Presentation
Modules Building Presentation
htyson
 

Similar to Integrating Doctrine with Laravel (20)

Hibernate in XPages
Hibernate in XPagesHibernate in XPages
Hibernate in XPages
 
They why behind php frameworks
They why behind php frameworksThey why behind php frameworks
They why behind php frameworks
 
New Persistence Features in Spring Roo 1.1
New Persistence Features in Spring Roo 1.1New Persistence Features in Spring Roo 1.1
New Persistence Features in Spring Roo 1.1
 
6 Months PHP internship in Noida
6 Months PHP internship in Noida6 Months PHP internship in Noida
6 Months PHP internship in Noida
 
Laravel ppt
Laravel pptLaravel ppt
Laravel ppt
 
Talend big data online training
Talend big data online trainingTalend big data online training
Talend big data online training
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
 
LDP4j: A framework for the development of interoperable read-write Linked Da...
LDP4j: A framework for the development of interoperable read-write Linked Da...LDP4j: A framework for the development of interoperable read-write Linked Da...
LDP4j: A framework for the development of interoperable read-write Linked Da...
 
Solr Recipes Workshop
Solr Recipes WorkshopSolr Recipes Workshop
Solr Recipes Workshop
 
Require.JS
Require.JSRequire.JS
Require.JS
 
Hibernate
HibernateHibernate
Hibernate
 
Drupal upgrades and migrations. BAD Camp 2013 version
Drupal upgrades and migrations. BAD Camp 2013 versionDrupal upgrades and migrations. BAD Camp 2013 version
Drupal upgrades and migrations. BAD Camp 2013 version
 
Integrating the Solr search engine
Integrating the Solr search engineIntegrating the Solr search engine
Integrating the Solr search engine
 
70487.pdf
70487.pdf70487.pdf
70487.pdf
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Modules Building Presentation
Modules Building PresentationModules Building Presentation
Modules Building Presentation
 
Doctrine2
Doctrine2Doctrine2
Doctrine2
 
Doctrine2 enterpice
Doctrine2 enterpiceDoctrine2 enterpice
Doctrine2 enterpice
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Integrating Doctrine with Laravel

  • 1. Integrating Doctrine with Laravel Mark Garratt - London Laravel Meetup - October 2015
  • 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
  • 7. LaravelDoctrine.org • Multiple entity managers and connections • Simple authentication implementation • Password reminders implementation • Doctrine console commands • DoctrineExtensions supported • Timestamps, Softdeletes and TablePrefix listeners
  • 8.
  • 9.
  • 10.
  • 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