Understanding Doctrine at True North PHP 2013

  • 2,676 views
Uploaded on

This is a set of slides purely for presentation along with my talk on "Understanding Doctrine" at True North PHP 2013. …

This is a set of slides purely for presentation along with my talk on "Understanding Doctrine" at True North PHP 2013.

The content of the presentation is available at https://github.com/shiroyuki/trphp13-demo/blob/master/notes/speaker-note.md.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,676
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
4
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Understanding Doctrine Juti Noppornpitak
  • 2. Today’s Menu • • • • • What is object relational mapping? How is Doctrine designed? Change Tracking Policy Proxies and Lazy loading DBAL or ORM? • • • • • • Cascading Doctrine Event System Second Level Cache Metadata Cache* Query Cache* Result Cache*
  • 3. Juti Noppornpitak Instaclick Inc., shiroyuki.com, @shiroyuki
  • 4. What is object relational mapping?
  • 5. ow is H ned? esig ine d Doctr
  • 6. PROBLEM Object-relational impedance mismatch
  • 7. DESIGN Data Mapper Pattern
  • 8. DESIGN Unit of Work
  • 9. DESIGN Repository Pattern
  • 10. Source: http://msdn.microsoft.com/en-us/library/ff649690.aspx
  • 11. Now, it is time for the entrée.
  • 12. Change Tracking Policy
  • 13. Implicit change tracking policy known as persisting by (UnitOfWork’s) reachability
  • 14. This means EntityManager’s persist method is disregarded and everything reachable/managed by the entity manager, including an initialized proxy, will be persisted automatically.
  • 15. Explicit change tracking policy
  • 16. This means the code has to explicitly persist to save the data.
  • 17. Notify change tracking policy
  • 18. This means the code has full control to explicitly tell UnitOfWork whether or not the entity is updated. ! So, even if there is an update, the change can be discarded as the notify method says there is no change.
  • 19. Proxies and Lazy Loading
  • 20. Everything is a proxy.
  • 21. What does it mean?
  • 22. Suppose you have m entities, named a1, a2, ... and am. During the course of code execution, we make a lot of queries. Each query makes at least one proxy per entity in the result set. Hence, the number of proxies will be around c×m proxies.
  • 23. Beside retrieving the ID of the proxy, retrieving or defining properties of a proxy always triggers the proxy loading if the proxy is set for lazy loading. ! In this situation, it might lead to making around cm queries by the end of the execution.
  • 24. Solutions
  • 25. Solution 1 Multiple SELECTs (ORM/DQL)
  • 26. SELECT FROM JOIN WHERE u, g User u u.groups g u.id = :id
  • 27. Solution 2 SELECT PARTIAL (ORM/DQL)
  • 28. SELECT PARTIAL u.{id, name} FROM User u WHERE u.id = :id or SELECT PARTIAL u.{id, name}, PARTIAL g.{id, name} FROM User u JOIN u.groups g WHERE u.id = :id
  • 29. Solution 3 Set the fetch mode to “eager”.
  • 30. But it is not recommended.
  • 31. Why?
  • 32. So, please do not try to use the “eager” mode.
  • 33. DBAL or ORM?
  • 34. Doctrine 2 Common bundle, DBAL bundle and ORM bundle
  • 35. ORM is convenient but we have to exchange resource for the convenience.
  • 36. Fetching as a hydrate array instead of an object graph.
  • 37. Database Abstract and Access Layer (DBAL) The thin runtime layer to low-level APIs, such as, PDO.
  • 38. When should I use DBAL?
  • 39. Why should I use the ORM first?
  • 40. C ascading
  • 41. Supported cascading operations DELETE, DETACH, MERGE and PERSIST
  • 42. Cascade on delete
  • 43. DOCTRINE E VENT SYSTEM
  • 44. Event Listeners
  • 45. Entity Listeners New in Doctrine ORM 2.4
  • 46. LifeCycle Event Callbacks It is the ugliest but best of all.
  • 47. Let’s leap into the future
  • 48. SECOND LEVEL CACHE
  • 49. How is the second level cache designed? for more information, see https://github.com/doctrine/doctrine2/pull/808
  • 50. The cache servers usually have faster read/write access than database servers.
  • 51. The cache servers only need to search from the smaller data set. This means it is faster to find the queried data.
  • 52. So, one obvious benefit of using the second level cache is that we reduce the access to the database.
  • 53. A few more things...
  • 54. Other kinds of cache They are not really visible but worth to mention.
  • 55. Metadata Cache
  • 56. Query Cache
  • 57. Result Cache
  • 58. CONCLUSION
  • 59. THE END 🌏 🐤 http://shiroyuki.com @shiroyuki