SlideShare a Scribd company logo
1 of 35
Download to read offline
Presentation title here
Doctrine internals
UnitOfWork
Presentation title here
About me
https://www.facebook.com/yatsenco
https://github.com/anyt
about me…
Andrey Yatsenco
● PHP Developer at Oro Inc.
● 3 years with Symfony
● 6 years with PHP
Presentation title here
Doctrine Internals. UnitOfWork
Introduction
● Doctrine is the bottleneck for many apps
● You know how UnitOfWork is working, you
know how doctrine works
Presentation title here
Doctrine Internals. UnitOfWork
UnitOfWork track objects changes and
commit them transactionally to the Database.
Now you don’t manage object loading from
the DataBase. UOW do.
Presentation title here
Doctrine Internals. UnitOfWork
UOW use IdentityMap to track objects
changes and to avoid server round trip
It simple two level array storage
● First level keys are ClassNames
● Second level keys are object IDs.
Presentation title here
Doctrine Internals. UnitOfWork
Doctrine EntityManager is like all-in-one
decorator on
● Repository
● UnitOfWork
● MetadataFactory
● ProxyFactory
● etc.
Presentation title here
Doctrine Internals. UnitOfWork
EntityManager -> EntityRepository
● find
● findOneBy
● findAll
● customFindQueryWithHidration
● etc.
Presentation title here
Doctrine Internals. UnitOfWork
EntityManager->UnitOfWork
● persist
● refresh
● remove
● flush
● etc.
Presentation title here
Doctrine Internals. UnitOfWork
EntityManager->UnitOfWork
● persist
● refresh
● remove
● commit
● etc.
Presentation title here
Doctrine Internals. UnitOfWork
Doctrine flow
● 1) Fetch entities from the database or
create new and persist ‘em (Repo|UOW)
● 2) Update some of entities directly or using
wrapper like Form (uses PropertyAccess)
● Remove some, if needed (Forms etc.)
● 3) EM->flush() changes (UOW)
Presentation title here
Doctrine Internals. UnitOfWork
1) Fetching entities from the DB
● One entity:
○ find
○ findOneBy
● Several entities:
○ Lazy/Eager loading
○ Find partial
Presentation title here
Doctrine Internals. UnitOfWork
1) Persist
● Can be omitted if you fetched entities from
doctrine
● Required only for newly created entities
Presentation title here
Doctrine Internals. UnitOfWork
2) Update
Object changes outside Doctrine.
● Direct changes
● Forms (PropertyAccessor)
● etc.
Presentation title here
Doctrine Internals. UnitOfWork
3) Flush
Sync changes with the Database
Presentation title here
Doctrine Internals. UnitOfWork
3) Flush Flow
● Open DB transaction
● Complute change-sets for update
● Iterate over scheduled inserts, updates,
deletes
● Delegate to entity persister SQL generation
and execution
● Clear UOW on success
● Close DB transaction
Presentation title here
Doctrine Internals. UnitOfWork
UnitOfWork through Doctrine Events
● fetch
● persist
● remove
● flush
Presentation title here
Doctrine Internals. UnitOfWork
Fetch
Repo-> find|refresh|getReference|etc.
● (optional) sync data with DB
● Events::loadClassMetadata
○ If ! Events:onClassMetadataNotFound
● hydrate
● Events:postLoad
Presentation title here
Doctrine Internals. UnitOfWork
Persist
● Events:prePersist
● Entity ID isn’t exist at this time in most cases
● UOW->scheduleForInsert($entity)
Presentation title here
Doctrine Internals. UnitOfWork
Remove
● Events::preRemove
● UOW->scheduleForDelete($entity)
Presentation title here
Doctrine Internals. UnitOfWork
Flush
● UOW->commit(null|object|array $entity)
Presentation title here
Doctrine Internals. UnitOfWork
Flush (1)
● Events::preFlush
○ $em->flush() can be called safely
● compute insert|update|delete changesets
● If !changes
○ Events::onFlush
○ Events::postFlush
○ return
Presentation title here
Doctrine Internals. UnitOfWork
Flush (2)
● Events::onFlush
○ UOW->getScheduledEntityInsertions()
○ UOW->getScheduledEntityUpdates()
○ UOW->getScheduledEntityDeletions()
○ $em->persist() && UOW-
>computeChangeSet()
● connection->beginTransaction
Presentation title here
Doctrine Internals. UnitOfWork
Flush (3)
● Inserts
○ Persister->insert($entity)
○ Events::postPersist
■ entity can’t be updated directly anymore
■ UOW->scheduleExtraUpdate($entity, array $changeset)
Presentation title here
Doctrine Internals. UnitOfWork
Flush (4)
● Updates
○ Events::preUpdate
■ Entity can’t be updated directly, update changeset using
PreUpdateEventArgs
■ Relations changes not tracked too
○ UOW->recomputeSingleEntityChangeSet($class, $entity)
○ Persister->update($entity)
○ Events::postUpdate
■ Entity can’t be updated directly anymore
■ UOW->scheduleExtraUpdate($entity, array $changeset)
● UOW->executeExtraUpdates()
Presentation title here
Doctrine Internals. UnitOfWork
Flush (5)
● Deletions
○ Persister->delete($entity)
○ Events::postRemove
■ nothing to update here
Presentation title here
Doctrine Internals. UnitOfWork
Flush (6)
● connection->commit()
● Events::postFlush
○ Never call $em->flush() here
● UOW->clear();
○ Events::onClear
● in case of errors
○ connection->rollback()
That’s it.
Presentation title here
Doctrine Internals. UnitOfWork
Let’s talk a little bit about performance and
how to speed-up doctrine
Presentation title here
Doctrine Internals. UnitOfWork
How to speed-up doctrine
● Cache
○ Metadata
○ Query
○ result
Presentation title here
Doctrine Internals. UnitOfWork
How to speed-up doctrine
● Control lazy-loading
○ Never eager load
○ Write select queries with joins to needed
Presentation title here
Doctrine Internals. UnitOfWork
How to speed-up doctrine
● Fetch less
○ getReference
○ {partial}
Presentation title here
Doctrine Internals. UnitOfWork
How to speed-up doctrine
● Hydrate less
○ arrays instead of objects when you don’t
need them or performance is important
Presentation title here
Doctrine Internals. UnitOfWork
How to speed-up doctrine
● Make flush cheaper
○ Mark entities as readOnly
○ Chang tracking policy to
DEFERRED_EXPLICIT or NOTIFY
○ Provide flush arguments (applies
objects and arrays)
Presentation title here
Doctrine Internals. UnitOfWork
How to speed-up doctrine
● Optimize queries
Presentation title here
Doctrine Internals. UnitOfWork
Links:
docs
● http://www.doctrine-project.org/api/orm/2.0/class-Doctrine.ORM.UnitOfWork.html
● http://doctrine-orm.readthedocs.io/projects/doctrine-orm/
Patterns
● http://martinfowler.com/eaaCatalog/unitOfWork.html
● http://martinfowler.com/eaaCatalog/identityMap.html
Sergey Zhuravel presentation about Doctrine Events
● http://www.slideshare.net/sergeyz/oro-meetups-doctrine-events?qid=7d9177e9-c41c-4642-
bdba-e1fa3060bb17&v=&b=&from_search=1
Presentation title here
?

More Related Content

What's hot

Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.Questpond
 
Programación Orientada a Objetos en Java - Parte I 2015
Programación Orientada a Objetos en Java - Parte I 2015Programación Orientada a Objetos en Java - Parte I 2015
Programación Orientada a Objetos en Java - Parte I 2015Renny Batista
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 
Encapsulamento em Orientação a Objetos
Encapsulamento em Orientação a ObjetosEncapsulamento em Orientação a Objetos
Encapsulamento em Orientação a ObjetosDaniel Brandão
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...Edureka!
 
Estrutura de Dados - Aula 03 - Ponteiros e Funções
Estrutura de Dados - Aula 03 - Ponteiros e FunçõesEstrutura de Dados - Aula 03 - Ponteiros e Funções
Estrutura de Dados - Aula 03 - Ponteiros e FunçõesLeinylson Fontinele
 
OroCommerce Storefront Design. Non-standard Layout Customisation.
OroCommerce Storefront Design. Non-standard Layout Customisation.OroCommerce Storefront Design. Non-standard Layout Customisation.
OroCommerce Storefront Design. Non-standard Layout Customisation.Andrew Yatsenko
 
Clean code - Mantenha seu código limpo
Clean code - Mantenha seu código limpoClean code - Mantenha seu código limpo
Clean code - Mantenha seu código limpoTiago Bencardino
 
javascript objects
javascript objectsjavascript objects
javascript objectsVijay Kalyan
 
Orientação a objetos em Python (compacto)
Orientação a objetos em Python (compacto)Orientação a objetos em Python (compacto)
Orientação a objetos em Python (compacto)Luciano Ramalho
 
Java: Composicao e Array List
Java: Composicao e Array ListJava: Composicao e Array List
Java: Composicao e Array ListArthur Emanuel
 
Prática de laboratório utilizando views, stored procedures e triggers
Prática de laboratório   utilizando views, stored procedures e triggersPrática de laboratório   utilizando views, stored procedures e triggers
Prática de laboratório utilizando views, stored procedures e triggersDaniel Maia
 

What's hot (20)

Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.
 
POO - 11 - Prática de Herança
POO - 11 - Prática de HerançaPOO - 11 - Prática de Herança
POO - 11 - Prática de Herança
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
15 Curso de POO en java - estructuras repetitivas
15 Curso de POO en java - estructuras repetitivas15 Curso de POO en java - estructuras repetitivas
15 Curso de POO en java - estructuras repetitivas
 
Programación Orientada a Objetos en Java - Parte I 2015
Programación Orientada a Objetos en Java - Parte I 2015Programación Orientada a Objetos en Java - Parte I 2015
Programación Orientada a Objetos en Java - Parte I 2015
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
Encapsulamento em Orientação a Objetos
Encapsulamento em Orientação a ObjetosEncapsulamento em Orientação a Objetos
Encapsulamento em Orientação a Objetos
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
 
Estrutura de Dados - Aula 03 - Ponteiros e Funções
Estrutura de Dados - Aula 03 - Ponteiros e FunçõesEstrutura de Dados - Aula 03 - Ponteiros e Funções
Estrutura de Dados - Aula 03 - Ponteiros e Funções
 
OroCommerce Storefront Design. Non-standard Layout Customisation.
OroCommerce Storefront Design. Non-standard Layout Customisation.OroCommerce Storefront Design. Non-standard Layout Customisation.
OroCommerce Storefront Design. Non-standard Layout Customisation.
 
Clean code - Mantenha seu código limpo
Clean code - Mantenha seu código limpoClean code - Mantenha seu código limpo
Clean code - Mantenha seu código limpo
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Orientação a objetos em Python (compacto)
Orientação a objetos em Python (compacto)Orientação a objetos em Python (compacto)
Orientação a objetos em Python (compacto)
 
Delegetes in c#
Delegetes in c#Delegetes in c#
Delegetes in c#
 
Java: Composicao e Array List
Java: Composicao e Array ListJava: Composicao e Array List
Java: Composicao e Array List
 
Prática de laboratório utilizando views, stored procedures e triggers
Prática de laboratório   utilizando views, stored procedures e triggersPrática de laboratório   utilizando views, stored procedures e triggers
Prática de laboratório utilizando views, stored procedures e triggers
 
UML
UMLUML
UML
 
JAVA - Herança
JAVA - HerançaJAVA - Herança
JAVA - Herança
 
Curso de Python e Django
Curso de Python e DjangoCurso de Python e Django
Curso de Python e Django
 

Similar to Doctrine Internals. UnitOfWork

Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsJeff Durta
 
Loaders (and why we should use them)
Loaders (and why we should use them)Loaders (and why we should use them)
Loaders (and why we should use them)Michael Pustovit
 
Automation Abstraction Layers: Page Objects and Beyond
Automation Abstraction Layers: Page Objects and BeyondAutomation Abstraction Layers: Page Objects and Beyond
Automation Abstraction Layers: Page Objects and BeyondAlan Richardson
 
Tool Development 07 - Undo & Redo, Drag & Drop
Tool Development 07 - Undo & Redo, Drag & DropTool Development 07 - Undo & Redo, Drag & Drop
Tool Development 07 - Undo & Redo, Drag & DropNick Pruehs
 
MobileCity:Core Data
MobileCity:Core DataMobileCity:Core Data
MobileCity:Core DataAllan Davis
 
Doctrine ORM Internals. UnitOfWork
Doctrine ORM Internals. UnitOfWorkDoctrine ORM Internals. UnitOfWork
Doctrine ORM Internals. UnitOfWorkIllia Antypenko
 
React Internals - How understanding React implementation can help us write be...
React Internals - How understanding React implementation can help us write be...React Internals - How understanding React implementation can help us write be...
React Internals - How understanding React implementation can help us write be...Ankit Muchhala
 
Deep dive - Concourse CI/CD and Pipelines
Deep dive  - Concourse CI/CD and PipelinesDeep dive  - Concourse CI/CD and Pipelines
Deep dive - Concourse CI/CD and PipelinesSyed Imam
 
How I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with AirflowHow I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with AirflowPyData
 
.NET @ apache.org
 .NET @ apache.org .NET @ apache.org
.NET @ apache.orgTed Husted
 
Performance #6 threading
Performance #6  threadingPerformance #6  threading
Performance #6 threadingVitali Pekelis
 
Symfony Form Basics - OroMeetup #3 Cherkassy
Symfony Form Basics - OroMeetup #3 CherkassySymfony Form Basics - OroMeetup #3 Cherkassy
Symfony Form Basics - OroMeetup #3 CherkassyAndrew Yatsenko
 
Building data flows with Celery and SQLAlchemy
Building data flows with Celery and SQLAlchemyBuilding data flows with Celery and SQLAlchemy
Building data flows with Celery and SQLAlchemyRoger Barnes
 
How I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with AirflowHow I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with AirflowLaura Lorenz
 
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples Ravi Mone
 
Customise Odoo addons modules
Customise Odoo addons modulesCustomise Odoo addons modules
Customise Odoo addons modulesGLC Networks
 
Background Processing With Work Manager
Background Processing With Work ManagerBackground Processing With Work Manager
Background Processing With Work ManagerSeven Peaks Speaks
 

Similar to Doctrine Internals. UnitOfWork (20)

Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
Loaders (and why we should use them)
Loaders (and why we should use them)Loaders (and why we should use them)
Loaders (and why we should use them)
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Automation Abstraction Layers: Page Objects and Beyond
Automation Abstraction Layers: Page Objects and BeyondAutomation Abstraction Layers: Page Objects and Beyond
Automation Abstraction Layers: Page Objects and Beyond
 
Tool Development 07 - Undo & Redo, Drag & Drop
Tool Development 07 - Undo & Redo, Drag & DropTool Development 07 - Undo & Redo, Drag & Drop
Tool Development 07 - Undo & Redo, Drag & Drop
 
MobileCity:Core Data
MobileCity:Core DataMobileCity:Core Data
MobileCity:Core Data
 
Doctrine ORM Internals. UnitOfWork
Doctrine ORM Internals. UnitOfWorkDoctrine ORM Internals. UnitOfWork
Doctrine ORM Internals. UnitOfWork
 
React Internals - How understanding React implementation can help us write be...
React Internals - How understanding React implementation can help us write be...React Internals - How understanding React implementation can help us write be...
React Internals - How understanding React implementation can help us write be...
 
React workshop
React workshopReact workshop
React workshop
 
Deep dive - Concourse CI/CD and Pipelines
Deep dive  - Concourse CI/CD and PipelinesDeep dive  - Concourse CI/CD and Pipelines
Deep dive - Concourse CI/CD and Pipelines
 
How I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with AirflowHow I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with Airflow
 
.NET @ apache.org
 .NET @ apache.org .NET @ apache.org
.NET @ apache.org
 
Performance #6 threading
Performance #6  threadingPerformance #6  threading
Performance #6 threading
 
Symfony Form Basics - OroMeetup #3 Cherkassy
Symfony Form Basics - OroMeetup #3 CherkassySymfony Form Basics - OroMeetup #3 Cherkassy
Symfony Form Basics - OroMeetup #3 Cherkassy
 
Building data flows with Celery and SQLAlchemy
Building data flows with Celery and SQLAlchemyBuilding data flows with Celery and SQLAlchemy
Building data flows with Celery and SQLAlchemy
 
How I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with AirflowHow I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with Airflow
 
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples
 
UPC Testing talk 2
UPC Testing talk 2UPC Testing talk 2
UPC Testing talk 2
 
Customise Odoo addons modules
Customise Odoo addons modulesCustomise Odoo addons modules
Customise Odoo addons modules
 
Background Processing With Work Manager
Background Processing With Work ManagerBackground Processing With Work Manager
Background Processing With Work Manager
 

More from Andrew Yatsenko

Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Andrew Yatsenko
 
Gear Up for OroPlatform 4.1 LTS. Dependency Injection Improvements Overview ...
Gear Up for OroPlatform 4.1 LTS.  Dependency Injection Improvements Overview ...Gear Up for OroPlatform 4.1 LTS.  Dependency Injection Improvements Overview ...
Gear Up for OroPlatform 4.1 LTS. Dependency Injection Improvements Overview ...Andrew Yatsenko
 
Effectively Reuse the Code Between PHP Projects
Effectively Reuse the Code Between PHP ProjectsEffectively Reuse the Code Between PHP Projects
Effectively Reuse the Code Between PHP ProjectsAndrew Yatsenko
 
Performance profiling and testing of symfony application 2
Performance profiling and testing of symfony application 2Performance profiling and testing of symfony application 2
Performance profiling and testing of symfony application 2Andrew Yatsenko
 
Data cache management in php
Data cache management in phpData cache management in php
Data cache management in phpAndrew Yatsenko
 
Writing extensible applications
Writing extensible applicationsWriting extensible applications
Writing extensible applicationsAndrew Yatsenko
 

More from Andrew Yatsenko (8)

Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
 
Gear Up for OroPlatform 4.1 LTS. Dependency Injection Improvements Overview ...
Gear Up for OroPlatform 4.1 LTS.  Dependency Injection Improvements Overview ...Gear Up for OroPlatform 4.1 LTS.  Dependency Injection Improvements Overview ...
Gear Up for OroPlatform 4.1 LTS. Dependency Injection Improvements Overview ...
 
Make the most of twig
Make the most of twigMake the most of twig
Make the most of twig
 
Effectively Reuse the Code Between PHP Projects
Effectively Reuse the Code Between PHP ProjectsEffectively Reuse the Code Between PHP Projects
Effectively Reuse the Code Between PHP Projects
 
Performance profiling and testing of symfony application 2
Performance profiling and testing of symfony application 2Performance profiling and testing of symfony application 2
Performance profiling and testing of symfony application 2
 
Using Oro layouts
Using Oro layoutsUsing Oro layouts
Using Oro layouts
 
Data cache management in php
Data cache management in phpData cache management in php
Data cache management in php
 
Writing extensible applications
Writing extensible applicationsWriting extensible applications
Writing extensible applications
 

Recently uploaded

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 

Recently uploaded (20)

(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 

Doctrine Internals. UnitOfWork

  • 1. Presentation title here Doctrine internals UnitOfWork
  • 2. Presentation title here About me https://www.facebook.com/yatsenco https://github.com/anyt about me… Andrey Yatsenco ● PHP Developer at Oro Inc. ● 3 years with Symfony ● 6 years with PHP
  • 3. Presentation title here Doctrine Internals. UnitOfWork Introduction ● Doctrine is the bottleneck for many apps ● You know how UnitOfWork is working, you know how doctrine works
  • 4. Presentation title here Doctrine Internals. UnitOfWork UnitOfWork track objects changes and commit them transactionally to the Database. Now you don’t manage object loading from the DataBase. UOW do.
  • 5. Presentation title here Doctrine Internals. UnitOfWork UOW use IdentityMap to track objects changes and to avoid server round trip It simple two level array storage ● First level keys are ClassNames ● Second level keys are object IDs.
  • 6. Presentation title here Doctrine Internals. UnitOfWork Doctrine EntityManager is like all-in-one decorator on ● Repository ● UnitOfWork ● MetadataFactory ● ProxyFactory ● etc.
  • 7. Presentation title here Doctrine Internals. UnitOfWork EntityManager -> EntityRepository ● find ● findOneBy ● findAll ● customFindQueryWithHidration ● etc.
  • 8. Presentation title here Doctrine Internals. UnitOfWork EntityManager->UnitOfWork ● persist ● refresh ● remove ● flush ● etc.
  • 9. Presentation title here Doctrine Internals. UnitOfWork EntityManager->UnitOfWork ● persist ● refresh ● remove ● commit ● etc.
  • 10. Presentation title here Doctrine Internals. UnitOfWork Doctrine flow ● 1) Fetch entities from the database or create new and persist ‘em (Repo|UOW) ● 2) Update some of entities directly or using wrapper like Form (uses PropertyAccess) ● Remove some, if needed (Forms etc.) ● 3) EM->flush() changes (UOW)
  • 11. Presentation title here Doctrine Internals. UnitOfWork 1) Fetching entities from the DB ● One entity: ○ find ○ findOneBy ● Several entities: ○ Lazy/Eager loading ○ Find partial
  • 12. Presentation title here Doctrine Internals. UnitOfWork 1) Persist ● Can be omitted if you fetched entities from doctrine ● Required only for newly created entities
  • 13. Presentation title here Doctrine Internals. UnitOfWork 2) Update Object changes outside Doctrine. ● Direct changes ● Forms (PropertyAccessor) ● etc.
  • 14. Presentation title here Doctrine Internals. UnitOfWork 3) Flush Sync changes with the Database
  • 15. Presentation title here Doctrine Internals. UnitOfWork 3) Flush Flow ● Open DB transaction ● Complute change-sets for update ● Iterate over scheduled inserts, updates, deletes ● Delegate to entity persister SQL generation and execution ● Clear UOW on success ● Close DB transaction
  • 16. Presentation title here Doctrine Internals. UnitOfWork UnitOfWork through Doctrine Events ● fetch ● persist ● remove ● flush
  • 17. Presentation title here Doctrine Internals. UnitOfWork Fetch Repo-> find|refresh|getReference|etc. ● (optional) sync data with DB ● Events::loadClassMetadata ○ If ! Events:onClassMetadataNotFound ● hydrate ● Events:postLoad
  • 18. Presentation title here Doctrine Internals. UnitOfWork Persist ● Events:prePersist ● Entity ID isn’t exist at this time in most cases ● UOW->scheduleForInsert($entity)
  • 19. Presentation title here Doctrine Internals. UnitOfWork Remove ● Events::preRemove ● UOW->scheduleForDelete($entity)
  • 20. Presentation title here Doctrine Internals. UnitOfWork Flush ● UOW->commit(null|object|array $entity)
  • 21. Presentation title here Doctrine Internals. UnitOfWork Flush (1) ● Events::preFlush ○ $em->flush() can be called safely ● compute insert|update|delete changesets ● If !changes ○ Events::onFlush ○ Events::postFlush ○ return
  • 22. Presentation title here Doctrine Internals. UnitOfWork Flush (2) ● Events::onFlush ○ UOW->getScheduledEntityInsertions() ○ UOW->getScheduledEntityUpdates() ○ UOW->getScheduledEntityDeletions() ○ $em->persist() && UOW- >computeChangeSet() ● connection->beginTransaction
  • 23. Presentation title here Doctrine Internals. UnitOfWork Flush (3) ● Inserts ○ Persister->insert($entity) ○ Events::postPersist ■ entity can’t be updated directly anymore ■ UOW->scheduleExtraUpdate($entity, array $changeset)
  • 24. Presentation title here Doctrine Internals. UnitOfWork Flush (4) ● Updates ○ Events::preUpdate ■ Entity can’t be updated directly, update changeset using PreUpdateEventArgs ■ Relations changes not tracked too ○ UOW->recomputeSingleEntityChangeSet($class, $entity) ○ Persister->update($entity) ○ Events::postUpdate ■ Entity can’t be updated directly anymore ■ UOW->scheduleExtraUpdate($entity, array $changeset) ● UOW->executeExtraUpdates()
  • 25. Presentation title here Doctrine Internals. UnitOfWork Flush (5) ● Deletions ○ Persister->delete($entity) ○ Events::postRemove ■ nothing to update here
  • 26. Presentation title here Doctrine Internals. UnitOfWork Flush (6) ● connection->commit() ● Events::postFlush ○ Never call $em->flush() here ● UOW->clear(); ○ Events::onClear ● in case of errors ○ connection->rollback() That’s it.
  • 27. Presentation title here Doctrine Internals. UnitOfWork Let’s talk a little bit about performance and how to speed-up doctrine
  • 28. Presentation title here Doctrine Internals. UnitOfWork How to speed-up doctrine ● Cache ○ Metadata ○ Query ○ result
  • 29. Presentation title here Doctrine Internals. UnitOfWork How to speed-up doctrine ● Control lazy-loading ○ Never eager load ○ Write select queries with joins to needed
  • 30. Presentation title here Doctrine Internals. UnitOfWork How to speed-up doctrine ● Fetch less ○ getReference ○ {partial}
  • 31. Presentation title here Doctrine Internals. UnitOfWork How to speed-up doctrine ● Hydrate less ○ arrays instead of objects when you don’t need them or performance is important
  • 32. Presentation title here Doctrine Internals. UnitOfWork How to speed-up doctrine ● Make flush cheaper ○ Mark entities as readOnly ○ Chang tracking policy to DEFERRED_EXPLICIT or NOTIFY ○ Provide flush arguments (applies objects and arrays)
  • 33. Presentation title here Doctrine Internals. UnitOfWork How to speed-up doctrine ● Optimize queries
  • 34. Presentation title here Doctrine Internals. UnitOfWork Links: docs ● http://www.doctrine-project.org/api/orm/2.0/class-Doctrine.ORM.UnitOfWork.html ● http://doctrine-orm.readthedocs.io/projects/doctrine-orm/ Patterns ● http://martinfowler.com/eaaCatalog/unitOfWork.html ● http://martinfowler.com/eaaCatalog/identityMap.html Sergey Zhuravel presentation about Doctrine Events ● http://www.slideshare.net/sergeyz/oro-meetups-doctrine-events?qid=7d9177e9-c41c-4642- bdba-e1fa3060bb17&v=&b=&from_search=1