2. What is Doctrine?
‣ Doctrine is a Object Relational Mapper built
to work with PHP 5.2.3 or greater.
‣ Primarily based off of Java Hibernate
‣ Influenced by Ruby on Rails ActiveRecord
From WikiPedia: http://en.wikipedia.org/wiki/Object-
relational_mapping
Doctrine 2 http://www.doctrine-project.org
4. Why should I use it?
‣ Makes difficult problems easy
Because
jwage said
‣ Saves money so!!!
‣ I like money
Doctrine 4 http://www.doctrine-project.org
5. Will it solve world hunger?
‣ No
‣ Will not solve all your problems
‣ Helps more than it hurts
Doctrine 5 http://www.doctrine-project.org
10. The examples in the next slides use the following models
Doctrine 10 http://www.doctrine-project.org
11. Doctrine Query Language
A object-oriented SQL-dialect used for retrieving data
‣ DQL makes complex SQL simple
‣ Brings OOP to your database queries
‣ Parsed and converted to SQL for your
dbms
‣ DQL parsing is cached
Doctrine 11 http://www.doctrine-project.org
12. DQL makes complex SQL simple
The DQL: FROM BlogPost p INNER JOIN p.Author a LEFT JOIN p.Tags t
The Resulting SQL: SELECT b.id AS b__id, b.title AS b__title, b.body AS
b__body, b.author_id AS b__author_id, b.slug AS b__slug, b.created_at AS
b__created_at, b.updated_at AS b__updated_at, a.id AS a__id, a.name AS
a__name, t.id AS t__id, t.name AS t__name FROM blog_post b INNER JOIN
author a ON b.author_id = a.id LEFT JOIN blog_post_tag b2 ON b.id =
b2.blog_post_id LEFT JOIN tag t ON t.id = b2.tag_id
Special select aliases created so Doctrine can hydrate the data
Doctrine 12 http://www.doctrine-project.org
13. Let Doctrine do the work
‣ You don’t need to know how things are
related, just that they are.
‣ Uses relationship information to
automatically fill in the blanks when
building SQL.
‣ Write complex queries very fast and
efficiently
Doctrine 13 http://www.doctrine-project.org
14. Executing the DQL
Query Results
Hydrated as
Multi-Dimensional
Array or Objects
Doctrine 14 http://www.doctrine-project.org
15. DBMS Functions
‣ DBMS functions passed through parser to SQL
‣ Any DBMS function can be used
‣ Propel short coming
‣ Can be used in WHERE, HAVING, etc.
Doctrine 15 http://www.doctrine-project.org
16. Named Queries
‣ Create named queries
‣ Execute named queries
‣ Retrieve named query objects
Doctrine 16 http://www.doctrine-project.org
17. Working with
Objects
Doctrine 17 http://www.doctrine-project.org
18. Accessors/Mutators
‣ 3 Different Styles
‣ Easy to use
Doctrine 18 http://www.doctrine-project.org
19. Overriding
‣ Override accessors and mutators easily
‣ Functions recognized and invoked with normal accessors
‣ Use _get()/_set() to avoid infinite loop
Doctrine 19 http://www.doctrine-project.org
20. Hydration Modes
‣ As objects
‣ As php arrays
‣ No hydration
Doctrine 20 http://www.doctrine-project.org
21. Array Access
Recommended
‣ Works with both
record and array
hydration methods
‣ Write code to
work with objects
and switch to
array hydration
without changing
code
‣ Performance
‣ Most familiar
Doctrine 21 http://www.doctrine-project.org
22. Simple Relationships
‣ Relations work the way
you’d expect it to
‣ Several different
relationship types
supported
‣ Specify relationships
inline
Doctrine 22 http://www.doctrine-project.org
23. Working with m2m
‣ Easy to link and unlink
‣ Specify new objects
inline
‣ Attach existing objects
Doctrine 23 http://www.doctrine-project.org
24. Updating
‣ Retrieve and update
‣ Update with one DQL
query
‣ DQL updates don’t issue
events/hooks for
updating
Doctrine 24 http://www.doctrine-project.org
25. Deleting
‣ Retrieve and
delete
‣ Delete without
retrieving
‣ DQL deletes
issue individual
queries
Doctrine 25 http://www.doctrine-project.org
26. Many2Many
‣ Reference table is used transparently
‣ No need to manually join reference table
‣ Easy to store extra data with reference
tables
Doctrine 26 http://www.doctrine-project.org
28. Many2Many Example 2
Simplified even more
Doctrine 28 http://www.doctrine-project.org
29. Friends List with Equal m2m
Doctrine 29 http://www.doctrine-project.org
30. Inspecting SQL of Equal M2M
SQL is generated with OR condition
so that relationship data is returned
on both sides
Doctrine 30 http://www.doctrine-project.org
31. Friends/Buddy List
Different SQL used so objects
which exist on one side, exist
automatically on the other
Now Fabien and I are friends!! Too easy!
Doctrine 31 http://www.doctrine-project.org
32. Plug n’ Play Behaviors
‣ Extract functionality
‣ Code re-usability
‣ Maintenance
‣ Time and money saver
‣ Write your own
‣ Offloads functionality to community
Doctrine 32 http://www.doctrine-project.org
37. The Results
Set automatically!
Updated!
Doctrine 37 http://www.doctrine-project.org
38. What Happened?
‣ Columns automatically added
‣ Automatically sets created_at and
update_at timestamps on save
‣ Automatic creation of unique, human
readable record identifier(slug)
FREE SOFTWARE! FREE FUNCTIONALITY!
ARE YOU SERIOUS?
Doctrine 38 http://www.doctrine-project.org
39. Data Fixtures
‣ Easy to specify
m2m data
‣ We did it first,
not rails ;)
Doctrine 39 http://www.doctrine-project.org
40. Data Fixtures Inline
‣ Specify data
fixtures inline
‣ More readable
‣ If a relationship
exists, you can
populate it inline
Doctrine 40 http://www.doctrine-project.org
41. Future
‣ Doctrine 1.1, 1.2.....2.0
‣ Separate packages for DBAL and ORM
‣ PEAR2: Replace MDB2? Defacto standard for
DBAL and ORM in PHP?
‣ Integration with many other libraries: symfony,
Zend Framework, Code Igniter, Typo3, etc.
Doctrine 41 http://www.doctrine-project.org
42. Doctrine 2.0
‣ Almost entirely rewritten code base
‣ Decoupling of components
‣ Off-loading of features to community: behaviors,
validation, yaml schema files, data fixtures, etc.
‣ Concentrate more on ORM specific functionality
Doctrine 42 http://www.doctrine-project.org
43. Want more?
‣ Read More
‣ http://www.doctrine-project.org/documentation/manual/1_0/?one-page#dql-
doctrine-query-language
‣ http://www.doctrine-project.org/documentation/manual/1_0/?one-page#migration
‣ http://www.doctrine-project.org/documentation/manual/1_0/?one-page#behaviors
‣ Community - http://www.doctrine-project.org/community
‣ Frequently Asked Questions - http://www.doctrine-project.org/faq
‣ About Doctrine - http://www.doctrine-project.org/about
‣ The Doctrine Blog - http://www.doctrine-project.org/blog
‣ Documentation - http://www.doctrine-project.org/documentation
Doctrine 43 http://www.doctrine-project.org
44. THE END!
I hope this presentation was helpful and sparked some
interest to play with Doctrine!
Follow the Doctrine...
Doctrine 44 http://www.doctrine-project.org