Upload
juti-noppornpitak
View
2.792
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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.
Citation preview
Understanding DoctrineJuti Noppornpitak
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*
Juti NoppornpitakInstaclick Inc., shiroyuki.com, @shiroyuki
What is object relational mapping?
How is Doctrine designed?
Object-relational impedance mismatchPROBLEM
Data Mapper Pattern
DESIGN
Unit of Work
DESIGN
Repository Pattern
DESIGN
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 meansEntityManager’s persist method is disregarded and
everything reachable/managed by the entity manager, including an initialized proxy, will be persisted
automatically.
Explicit change tracking policy
This meansthe code has to explicitly persist to save the data.
Notify change tracking policy
This meansthe 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.
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 query makes at least one proxy per entity in the result set.
Hence, the number of proxies will be around c×m proxies.
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.
Solutions
Solution 1Multiple SELECTs (ORM/DQL)
SELECT u, g FROM User u JOIN u.groups g WHERE u.id = :id
Solution 2SELECT PARTIAL (ORM/DQL)
SELECT PARTIAL u.{id, name} FROM User u WHERE u.id = :id
SELECT PARTIAL u.{id, name}, PARTIAL g.{id, name} FROM User u JOIN u.groups g WHERE u.id = :id
or
Solution 3Set 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 2Common bundle, DBAL bundle and ORM bundle
ORM is convenientbut we have to exchange resource for the convenience.
Fetching as a hydrate arrayinstead 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?
ascadingC
Supported cascading operations
DELETE, DETACH, MERGE and PERSIST
Cascade on delete
EDOCTRINE VENT SYSTEM
Event Listeners
Entity ListenersNew 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 cacheThey are not really visible but worth to mention.
Metadata Cache
Query Cache
Result Cache
CONCLUSION