understanding doctrine at true north php 2013

69
Understanding Doctrine Juti Noppornpitak

Upload: juti-noppornpitak

Post on 04-Jun-2015

2.795 views

Category:

Technology


0 download

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.

TRANSCRIPT

Page 1: Understanding Doctrine at True North PHP 2013

Understanding DoctrineJuti Noppornpitak

Page 2: Understanding Doctrine at True North PHP 2013

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*

Page 3: Understanding Doctrine at True North PHP 2013

Juti NoppornpitakInstaclick Inc., shiroyuki.com, @shiroyuki

Page 4: Understanding Doctrine at True North PHP 2013

What is object relational mapping?

Page 5: Understanding Doctrine at True North PHP 2013

How is Doctrine designed?

Page 6: Understanding Doctrine at True North PHP 2013

Object-relational impedance mismatchPROBLEM

Page 7: Understanding Doctrine at True North PHP 2013

Data Mapper Pattern

DESIGN

Page 8: Understanding Doctrine at True North PHP 2013

Unit of Work

DESIGN

Page 9: Understanding Doctrine at True North PHP 2013

Repository Pattern

DESIGN

Page 10: Understanding Doctrine at True North PHP 2013

Source: http://msdn.microsoft.com/en-us/library/ff649690.aspx

Page 11: Understanding Doctrine at True North PHP 2013
Page 12: Understanding Doctrine at True North PHP 2013

Now, it is time for the entrée.

Page 13: Understanding Doctrine at True North PHP 2013

Change Tracking Policy

Page 14: Understanding Doctrine at True North PHP 2013

Implicit change tracking policy

known as persisting by (UnitOfWork’s) reachability

Page 15: Understanding Doctrine at True North PHP 2013

This meansEntityManager’s persist method is disregarded and

everything reachable/managed by the entity manager, including an initialized proxy, will be persisted

automatically.

Page 16: Understanding Doctrine at True North PHP 2013

Explicit change tracking policy

Page 17: Understanding Doctrine at True North PHP 2013

This meansthe code has to explicitly persist to save the data.

Page 18: Understanding Doctrine at True North PHP 2013

Notify change tracking policy

Page 19: Understanding Doctrine at True North PHP 2013

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.

Page 20: Understanding Doctrine at True North PHP 2013

Proxies and Lazy Loading

Page 21: Understanding Doctrine at True North PHP 2013

Everything is a proxy.

Page 22: Understanding Doctrine at True North PHP 2013

What does it mean?

Page 23: Understanding Doctrine at True North PHP 2013

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.

Page 24: Understanding Doctrine at True North PHP 2013

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.

Page 25: Understanding Doctrine at True North PHP 2013

Solutions

Page 26: Understanding Doctrine at True North PHP 2013

Solution 1Multiple SELECTs (ORM/DQL)

Page 27: Understanding Doctrine at True North PHP 2013

SELECT u, g FROM User u JOIN u.groups g WHERE u.id = :id

Page 28: Understanding Doctrine at True North PHP 2013

Solution 2SELECT PARTIAL (ORM/DQL)

Page 29: Understanding Doctrine at True North PHP 2013

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

Page 30: Understanding Doctrine at True North PHP 2013

Solution 3Set the fetch mode to “eager”.

Page 31: Understanding Doctrine at True North PHP 2013

But it is not recommended.

Page 32: Understanding Doctrine at True North PHP 2013

Why?

Page 33: Understanding Doctrine at True North PHP 2013
Page 34: Understanding Doctrine at True North PHP 2013
Page 35: Understanding Doctrine at True North PHP 2013
Page 36: Understanding Doctrine at True North PHP 2013
Page 37: Understanding Doctrine at True North PHP 2013
Page 38: Understanding Doctrine at True North PHP 2013

So, please do not try to use the “eager” mode.

Page 39: Understanding Doctrine at True North PHP 2013

DBAL or ORM?

Page 40: Understanding Doctrine at True North PHP 2013

Doctrine 2Common bundle, DBAL bundle and ORM bundle

Page 41: Understanding Doctrine at True North PHP 2013

ORM is convenientbut we have to exchange resource for the convenience.

Page 42: Understanding Doctrine at True North PHP 2013

Fetching as a hydrate arrayinstead of an object graph.

Page 43: Understanding Doctrine at True North PHP 2013
Page 44: Understanding Doctrine at True North PHP 2013

Database Abstract and Access Layer (DBAL)The thin runtime layer to low-level APIs, such as, PDO.

Page 45: Understanding Doctrine at True North PHP 2013

When should I use DBAL?

Page 46: Understanding Doctrine at True North PHP 2013

Why should I use the ORM first?

Page 47: Understanding Doctrine at True North PHP 2013

ascadingC

Page 48: Understanding Doctrine at True North PHP 2013

Supported cascading operations

DELETE, DETACH, MERGE and PERSIST

Page 49: Understanding Doctrine at True North PHP 2013

Cascade on delete

Page 50: Understanding Doctrine at True North PHP 2013

EDOCTRINE VENT SYSTEM

Page 51: Understanding Doctrine at True North PHP 2013

Event Listeners

Page 52: Understanding Doctrine at True North PHP 2013
Page 53: Understanding Doctrine at True North PHP 2013

Entity ListenersNew in Doctrine ORM 2.4

Page 54: Understanding Doctrine at True North PHP 2013
Page 55: Understanding Doctrine at True North PHP 2013

LifeCycle Event Callbacks

It is the ugliest but best of all.

Page 56: Understanding Doctrine at True North PHP 2013
Page 57: Understanding Doctrine at True North PHP 2013

Let’s leap into the

future

Page 58: Understanding Doctrine at True North PHP 2013

SECOND LEVEL CACHE

Page 59: Understanding Doctrine at True North PHP 2013

How is the second level cache designed?for more information, see https://github.com/doctrine/doctrine2/pull/808

Page 60: Understanding Doctrine at True North PHP 2013

The cache servers usually have faster read/write access

than database servers.

Page 61: Understanding Doctrine at True North PHP 2013

The cache servers only need to search from the smaller data set. This means it is faster to find the

queried data.

Page 62: Understanding Doctrine at True North PHP 2013

So, one obvious benefit of using the second level cache is that we

reduce the access to the database.

Page 63: Understanding Doctrine at True North PHP 2013

A few more things...

Page 64: Understanding Doctrine at True North PHP 2013

Other kinds of cacheThey are not really visible but worth to mention.

Page 65: Understanding Doctrine at True North PHP 2013

Metadata Cache

Page 66: Understanding Doctrine at True North PHP 2013

Query Cache

Page 67: Understanding Doctrine at True North PHP 2013

Result Cache

Page 68: Understanding Doctrine at True North PHP 2013

CONCLUSION

Page 69: Understanding Doctrine at True North PHP 2013

THE END

🌏 http://shiroyuki.com 🐤 @shiroyuki