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
?

Doctrine Internals. UnitOfWork

  • 1.
  • 2.
    Presentation title here Aboutme 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 DoctrineInternals. UnitOfWork Introduction ● Doctrine is the bottleneck for many apps ● You know how UnitOfWork is working, you know how doctrine works
  • 4.
    Presentation title here DoctrineInternals. 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 DoctrineInternals. 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 DoctrineInternals. UnitOfWork Doctrine EntityManager is like all-in-one decorator on ● Repository ● UnitOfWork ● MetadataFactory ● ProxyFactory ● etc.
  • 7.
    Presentation title here DoctrineInternals. UnitOfWork EntityManager -> EntityRepository ● find ● findOneBy ● findAll ● customFindQueryWithHidration ● etc.
  • 8.
    Presentation title here DoctrineInternals. UnitOfWork EntityManager->UnitOfWork ● persist ● refresh ● remove ● flush ● etc.
  • 9.
    Presentation title here DoctrineInternals. UnitOfWork EntityManager->UnitOfWork ● persist ● refresh ● remove ● commit ● etc.
  • 10.
    Presentation title here DoctrineInternals. 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 DoctrineInternals. UnitOfWork 1) Fetching entities from the DB ● One entity: ○ find ○ findOneBy ● Several entities: ○ Lazy/Eager loading ○ Find partial
  • 12.
    Presentation title here DoctrineInternals. UnitOfWork 1) Persist ● Can be omitted if you fetched entities from doctrine ● Required only for newly created entities
  • 13.
    Presentation title here DoctrineInternals. UnitOfWork 2) Update Object changes outside Doctrine. ● Direct changes ● Forms (PropertyAccessor) ● etc.
  • 14.
    Presentation title here DoctrineInternals. UnitOfWork 3) Flush Sync changes with the Database
  • 15.
    Presentation title here DoctrineInternals. 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 DoctrineInternals. UnitOfWork UnitOfWork through Doctrine Events ● fetch ● persist ● remove ● flush
  • 17.
    Presentation title here DoctrineInternals. UnitOfWork Fetch Repo-> find|refresh|getReference|etc. ● (optional) sync data with DB ● Events::loadClassMetadata ○ If ! Events:onClassMetadataNotFound ● hydrate ● Events:postLoad
  • 18.
    Presentation title here DoctrineInternals. UnitOfWork Persist ● Events:prePersist ● Entity ID isn’t exist at this time in most cases ● UOW->scheduleForInsert($entity)
  • 19.
    Presentation title here DoctrineInternals. UnitOfWork Remove ● Events::preRemove ● UOW->scheduleForDelete($entity)
  • 20.
    Presentation title here DoctrineInternals. UnitOfWork Flush ● UOW->commit(null|object|array $entity)
  • 21.
    Presentation title here DoctrineInternals. 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 DoctrineInternals. UnitOfWork Flush (2) ● Events::onFlush ○ UOW->getScheduledEntityInsertions() ○ UOW->getScheduledEntityUpdates() ○ UOW->getScheduledEntityDeletions() ○ $em->persist() && UOW- >computeChangeSet() ● connection->beginTransaction
  • 23.
    Presentation title here DoctrineInternals. 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 DoctrineInternals. 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 DoctrineInternals. UnitOfWork Flush (5) ● Deletions ○ Persister->delete($entity) ○ Events::postRemove ■ nothing to update here
  • 26.
    Presentation title here DoctrineInternals. 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 DoctrineInternals. UnitOfWork Let’s talk a little bit about performance and how to speed-up doctrine
  • 28.
    Presentation title here DoctrineInternals. UnitOfWork How to speed-up doctrine ● Cache ○ Metadata ○ Query ○ result
  • 29.
    Presentation title here DoctrineInternals. UnitOfWork How to speed-up doctrine ● Control lazy-loading ○ Never eager load ○ Write select queries with joins to needed
  • 30.
    Presentation title here DoctrineInternals. UnitOfWork How to speed-up doctrine ● Fetch less ○ getReference ○ {partial}
  • 31.
    Presentation title here DoctrineInternals. 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 DoctrineInternals. 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 DoctrineInternals. UnitOfWork How to speed-up doctrine ● Optimize queries
  • 34.
    Presentation title here DoctrineInternals. 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
  • 35.