Download - Doctrine Php Object Relational Mapper
Doctrine http://www.doctrine-project.org
Doctrine, PHP Object Relational Mapper
1
Doctrine http://www.doctrine-project.org
What is Doctrine?
From WikiPedia: http://en.wikipedia.org/wiki/Object-relational_mapping
2
‣ 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
Doctrine http://www.doctrine-project.org
The Layers
3
Doctrine http://www.doctrine-project.org
Why should I use it?
Because jwage said
so!!!
‣ Makes difficult problems easy
‣ Saves money
‣ I like money
4
Doctrine http://www.doctrine-project.org
Will it solve world hunger?
‣ No
‣Will not solve all your problems
‣ Helps more than it hurts
5
Doctrine http://www.doctrine-project.org
1, 2, 3, Let’s go!
6
Doctrine http://www.doctrine-project.org7
Doctrine http://www.doctrine-project.org8
Doctrine http://www.doctrine-project.org9
Doctrine http://www.doctrine-project.org
The examples in the next slides use the following models
10
Doctrine http://www.doctrine-project.org
Doctrine Query LanguageA 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
11
Doctrine http://www.doctrine-project.org
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
12
Special select aliases created so Doctrine can hydrate the data
Doctrine http://www.doctrine-project.org13
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 http://www.doctrine-project.org
Executing the DQL
14
Query ResultsHydrated as
Multi-Dimensional Array or Objects
Doctrine http://www.doctrine-project.org15
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 http://www.doctrine-project.org16
Named Queries‣ Create named queries
‣ Execute named queries
‣ Retrieve named query objects
Doctrine http://www.doctrine-project.org
Working with Objects
17
Doctrine http://www.doctrine-project.org18
Accessors/Mutators
‣ 3 Different Styles
‣ Easy to use
Doctrine http://www.doctrine-project.org19
Overriding‣ Override accessors and mutators easily
‣ Functions recognized and invoked with normal accessors
‣ Use _get()/_set() to avoid infinite loop
Doctrine http://www.doctrine-project.org20
Hydration Modes‣ As objects
‣ As php arrays
‣ No hydration
Doctrine http://www.doctrine-project.org21
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 http://www.doctrine-project.org22
Simple Relationships
‣ Relations work the way you’d expect it to
‣ Several different relationship types supported
‣ Specify relationships inline
Doctrine http://www.doctrine-project.org23
Working with m2m
‣ Easy to link and unlink
‣ Specify new objects inline
‣ Attach existing objects
Doctrine http://www.doctrine-project.org24
Updating
‣ Retrieve and update
‣ Update with one DQL query
‣ DQL updates don’t issue events/hooks for updating
Doctrine http://www.doctrine-project.org25
Deleting
‣ Retrieve and delete
‣ Delete without retrieving
‣ DQL deletes issue individual queries
Doctrine http://www.doctrine-project.org26
Many2Many
‣ Reference table is used transparently
‣ No need to manually join reference table
‣ Easy to store extra data with reference tables
Doctrine http://www.doctrine-project.org27
Many2Many Example 1
Doctrine http://www.doctrine-project.org28
Many2Many Example 2Simplified even more
Doctrine http://www.doctrine-project.org29
Friends List with Equal m2m
Doctrine http://www.doctrine-project.org30
Inspecting SQL of Equal M2M
SQL is generated with OR condition so that relationship data is returned on both sides
Doctrine http://www.doctrine-project.org31
Friends/Buddy ListDifferent SQL used so objects which exist on one side, exist automatically on the other
Now Fabien and I are friends!! Too easy!
Doctrine http://www.doctrine-project.org
Plug n’ Play Behaviors‣ Extract functionality
‣ Code re-usability
‣ Maintenance
‣ Time and money saver
‣ Write your own
‣ Offloads functionality to community32
Doctrine http://www.doctrine-project.org
Core Behaviors
‣ Timestampable‣ Sluggable‣ Versionable‣ I18n‣ SoftDelete‣ NestedSet‣ Geographical
33
Doctrine http://www.doctrine-project.org
Real world exampleUsing the Sluggable and Timestampable behaviors for a BlogPost model
34
Doctrine http://www.doctrine-project.org
The Create Table SQL
35
Doctrine http://www.doctrine-project.org
Behavior In Action
36
Doctrine http://www.doctrine-project.org
The Results
37
Set automatically!
Updated!
Doctrine http://www.doctrine-project.org
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?
38
Doctrine http://www.doctrine-project.org39
Data Fixtures
‣ Easy to specify m2m data
‣ We did it first, not rails ;)
Doctrine http://www.doctrine-project.org40
Data Fixtures Inline
‣ Specify data fixtures inline
‣ More readable
‣ If a relationship exists, you can populate it inline
Doctrine http://www.doctrine-project.org41
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 http://www.doctrine-project.org42
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 http://www.doctrine-project.org
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
43
Doctrine http://www.doctrine-project.org
THE END!
I hope this presentation was helpful and sparked some interest to play with Doctrine!
Follow the Doctrine...
44