Understanding Doctrine
Juti Noppornpitak
Today’s Menu

•
•
•
•
•

What is object relational
mapping?
How is Doctrine designed?
Change Tracking Policy
Proxies and L...
Juti Noppornpitak
Instaclick Inc., shiroyuki.com, @shiroyuki
What is object relational mapping?
ow is
H

ned?
esig
ine d
Doctr
PROBLEM

Object-relational
impedance mismatch
DESIGN

Data Mapper Pattern
DESIGN

Unit of Work
DESIGN

Repository Pattern
Source: http://msdn.microsoft.com/en-us/library/ff649690.aspx
Now, it is time for the entrée.
Change Tracking Policy
Implicit change
tracking policy
known as persisting by (UnitOfWork’s) reachability
This means
EntityManager’s persist method is disregarded and
everything reachable/managed by the entity manager,
including...
Explicit change
tracking policy
This means
the code has to explicitly persist to save the data.
Notify change tracking
policy
This means
the code has full control to explicitly tell UnitOfWork
whether or not the entity is updated.
!

So, even if th...
Proxies
and Lazy
Loading
Everything is a proxy.
What does it mean?
Suppose you have m entities, named a1, a2, ...
and am. During the course of code execution,
we make a lot of queries. Each...
Beside retrieving the ID of the proxy, retrieving or
defining properties of a proxy always triggers the
proxy loading if th...
Solutions
Solution 1
Multiple SELECTs (ORM/DQL)
SELECT
FROM
JOIN
WHERE

u, g	
User u	
u.groups g	
u.id = :id
Solution 2
SELECT PARTIAL (ORM/DQL)
SELECT PARTIAL u.{id, name}	
FROM User u	
WHERE u.id = :id
or
SELECT PARTIAL u.{id, name},	
PARTIAL g.{id, name}	
FROM Use...
Solution 3
Set the fetch mode to “eager”.
But it is not
recommended.
Why?
So, please do not try to use the “eager” mode.
DBAL or ORM?
Doctrine 2
Common bundle, DBAL bundle and ORM bundle
ORM is convenient
but we have to exchange resource for the convenience.
Fetching as a hydrate array
instead of an object graph.
Database Abstract and
Access Layer (DBAL)
The thin runtime layer to low-level APIs, such as, PDO.
When should I use DBAL?
Why should I use the
ORM first?
C

ascading
Supported cascading
operations
DELETE, DETACH, MERGE and PERSIST
Cascade on delete
DOCTRINE

E

VENT SYSTEM
Event Listeners
Entity Listeners
New in Doctrine ORM 2.4
LifeCycle Event
Callbacks
It is the ugliest but best of all.
Let’s leap into the

future
SECOND
LEVEL
CACHE
How is the second
level cache designed?
for more information, see https://github.com/doctrine/doctrine2/pull/808
The cache servers usually
have faster read/write access
than database servers.
The cache servers only need to
search from the smaller data set.
This means it is faster to find the
queried data.
So, one obvious benefit of using
the second level cache is that we
reduce the access to the
database.
A few more things...
Other kinds of cache
They are not really visible but worth to mention.
Metadata Cache
Query Cache
Result Cache
CONCLUSION
THE END

🌏
🐤

http://shiroyuki.com
@shiroyuki
Understanding Doctrine at True North PHP 2013
Understanding Doctrine at True North PHP 2013
Understanding Doctrine at True North PHP 2013
Understanding Doctrine at True North PHP 2013
Understanding Doctrine at True North PHP 2013
Understanding Doctrine at True North PHP 2013
Understanding Doctrine at True North PHP 2013
Understanding Doctrine at True North PHP 2013
Understanding Doctrine at True North PHP 2013
Understanding Doctrine at True North PHP 2013
Upcoming SlideShare
Loading in …5
×

Understanding Doctrine at True North PHP 2013

3,311 views

Published on

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.

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,311
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
9
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Understanding Doctrine at True North PHP 2013

  1. 1. Understanding Doctrine Juti Noppornpitak
  2. 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. 3. Juti Noppornpitak Instaclick Inc., shiroyuki.com, @shiroyuki
  4. 4. What is object relational mapping?
  5. 5. ow is H ned? esig ine d Doctr
  6. 6. PROBLEM Object-relational impedance mismatch
  7. 7. DESIGN Data Mapper Pattern
  8. 8. DESIGN Unit of Work
  9. 9. DESIGN Repository Pattern
  10. 10. Source: http://msdn.microsoft.com/en-us/library/ff649690.aspx
  11. 11. Now, it is time for the entrée.
  12. 12. Change Tracking Policy
  13. 13. Implicit change tracking policy known as persisting by (UnitOfWork’s) reachability
  14. 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. 15. Explicit change tracking policy
  16. 16. This means the code has to explicitly persist to save the data.
  17. 17. Notify change tracking policy
  18. 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. 19. Proxies and Lazy Loading
  20. 20. Everything is a proxy.
  21. 21. What does it mean?
  22. 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. 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. 24. Solutions
  25. 25. Solution 1 Multiple SELECTs (ORM/DQL)
  26. 26. SELECT FROM JOIN WHERE u, g User u u.groups g u.id = :id
  27. 27. Solution 2 SELECT PARTIAL (ORM/DQL)
  28. 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. 29. Solution 3 Set the fetch mode to “eager”.
  30. 30. But it is not recommended.
  31. 31. Why?
  32. 32. So, please do not try to use the “eager” mode.
  33. 33. DBAL or ORM?
  34. 34. Doctrine 2 Common bundle, DBAL bundle and ORM bundle
  35. 35. ORM is convenient but we have to exchange resource for the convenience.
  36. 36. Fetching as a hydrate array instead of an object graph.
  37. 37. Database Abstract and Access Layer (DBAL) The thin runtime layer to low-level APIs, such as, PDO.
  38. 38. When should I use DBAL?
  39. 39. Why should I use the ORM first?
  40. 40. C ascading
  41. 41. Supported cascading operations DELETE, DETACH, MERGE and PERSIST
  42. 42. Cascade on delete
  43. 43. DOCTRINE E VENT SYSTEM
  44. 44. Event Listeners
  45. 45. Entity Listeners New in Doctrine ORM 2.4
  46. 46. LifeCycle Event Callbacks It is the ugliest but best of all.
  47. 47. Let’s leap into the future
  48. 48. SECOND LEVEL CACHE
  49. 49. How is the second level cache designed? for more information, see https://github.com/doctrine/doctrine2/pull/808
  50. 50. The cache servers usually have faster read/write access than database servers.
  51. 51. The cache servers only need to search from the smaller data set. This means it is faster to find the queried data.
  52. 52. So, one obvious benefit of using the second level cache is that we reduce the access to the database.
  53. 53. A few more things...
  54. 54. Other kinds of cache They are not really visible but worth to mention.
  55. 55. Metadata Cache
  56. 56. Query Cache
  57. 57. Result Cache
  58. 58. CONCLUSION
  59. 59. THE END 🌏 🐤 http://shiroyuki.com @shiroyuki

×