Doctrine, PHP Object
 Relational Mapper


Doctrine   1   http://www.doctrine-project.org
What is Doctrine?
‣ Doctrine is a Object Relational Mapper built
  to work with PHP 5.2.3 or greater.

‣ Primarily based o...
The Layers




Doctrine       3        http://www.doctrine-project.org
Why should I use it?

 ‣ Makes difficult problems easy
                                Because
                            ...
Will it solve world hunger?

‣ No
‣ Will not solve all your problems
‣ Helps more than it hurts


Doctrine         5      ...
1, 2, 3, Let’s go!


Doctrine   6   http://www.doctrine-project.org
Doctrine   7   http://www.doctrine-project.org
Doctrine   8   http://www.doctrine-project.org
Doctrine   9   http://www.doctrine-project.org
The examples in the next slides use the following models




Doctrine                   10               http://www.doctri...
Doctrine Query Language
    A object-oriented SQL-dialect used for retrieving data


‣ DQL makes complex SQL simple
‣ Brin...
DQL makes complex SQL simple



The DQL: FROM BlogPost p INNER JOIN p.Author a LEFT JOIN p.Tags t

The Resulting SQL: SELE...
Let Doctrine do the work

‣ You don’t need to know how things are
  related, just that they are.

‣ Uses relationship info...
Executing the DQL
 Query Results
  Hydrated as
Multi-Dimensional
Array or Objects




Doctrine            14   http://www....
DBMS Functions
 ‣ DBMS functions passed through parser to SQL
 ‣ Any DBMS function can be used
 ‣ Propel short coming
 ‣ C...
Named Queries
 ‣ Create named queries
 ‣ Execute named queries
 ‣ Retrieve named query objects




Doctrine            16 ...
Working with
       Objects


Doctrine   17   http://www.doctrine-project.org
Accessors/Mutators
 ‣ 3 Different Styles
 ‣ Easy to use




Doctrine         18     http://www.doctrine-project.org
Overriding
‣ Override accessors and mutators easily
‣ Functions recognized and invoked with normal accessors
‣ Use _get()/...
Hydration Modes
 ‣ As objects
 ‣ As php arrays
 ‣ No hydration


Doctrine          20   http://www.doctrine-project.org
Array Access
              Recommended
‣ Works with both
   record and array
   hydration methods

‣ Write code to
   work...
Simple Relationships

‣ Relations work the way
   you’d expect it to

‣ Several different
   relationship types
   support...
Working with m2m


‣ Easy to link and unlink
‣ Specify new objects
   inline

‣ Attach existing objects




   Doctrine   ...
Updating

‣ Retrieve and update
‣ Update with one DQL
   query

‣ DQL updates don’t issue
   events/hooks for
   updating
...
Deleting
‣ Retrieve and
  delete

‣ Delete without
  retrieving

‣ DQL deletes
  issue individual
  queries



   Doctrine...
Many2Many

‣ Reference table is used transparently
‣ No need to manually join reference table
‣ Easy to store extra data w...
Many2Many Example 1




Doctrine   27   http://www.doctrine-project.org
Many2Many Example 2
           Simplified even more




Doctrine           28            http://www.doctrine-project.org
Friends List with Equal m2m




Doctrine     29     http://www.doctrine-project.org
Inspecting SQL of Equal M2M




       SQL is generated with OR condition
       so that relationship data is returned
   ...
Friends/Buddy List
Different SQL used so objects
which exist on one side, exist
automatically on the other




        Now...
Plug n’ Play Behaviors
 ‣ Extract functionality
 ‣ Code re-usability
 ‣ Maintenance
 ‣ Time and money saver
 ‣ Write your ...
Core Behaviors
 ‣ Timestampable
 ‣ Sluggable
 ‣ Versionable
 ‣ I18n
 ‣ SoftDelete
 ‣ NestedSet
 ‣ Geographical
Doctrine   ...
Real world example
   Using the Sluggable and Timestampable behaviors for a BlogPost model




Doctrine                   ...
The Create Table SQL




Doctrine   35   http://www.doctrine-project.org
Behavior In Action




Doctrine   36     http://www.doctrine-project.org
The Results


                     Set automatically!




                     Updated!



Doctrine        37         http...
What Happened?
‣ Columns automatically added
‣ Automatically sets created_at and
    update_at timestamps on save
‣   Auto...
Data Fixtures

‣ Easy to specify
  m2m data

‣ We did it first,
  not rails ;)




 Doctrine           39   http://www.doct...
Data Fixtures Inline

‣ Specify data
  fixtures inline

‣ More readable
‣ If a relationship
  exists, you can
  populate it...
Future
‣ Doctrine 1.1, 1.2.....2.0
‣ Separate packages for DBAL and ORM
‣ PEAR2: Replace MDB2? Defacto standard for
  DBAL...
Doctrine 2.0

‣ Almost entirely rewritten code base
‣ Decoupling of components
‣ Off-loading of features to community: beh...
Want more?
‣ Read More
  ‣ http://www.doctrine-project.org/documentation/manual/1_0/?one-page#dql-
      doctrine-query-la...
THE END!
I hope this presentation was helpful and sparked some
             interest to play with Doctrine!




          ...
Upcoming SlideShare
Loading in...5
×

Doctrine Php Object Relational Mapper

5,700

Published on

Doctrine 1.0 presentation from Symfony Camp 2008

1 Comment
2 Likes
Statistics
Notes
  • I'm new to Doctrine ORM and I'm fighting a bit with schema definitions (I've been using CakePHP). I found ORM Designer www.orm-designer.com which seems to handle Doctrine very well but is commercial. Are you guys planning to release something similar just for Doctrine?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
5,700
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
100
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Doctrine Php Object Relational Mapper"

  1. 1. Doctrine, PHP Object Relational Mapper Doctrine 1 http://www.doctrine-project.org
  2. 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
  3. 3. The Layers Doctrine 3 http://www.doctrine-project.org
  4. 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. 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
  6. 6. 1, 2, 3, Let’s go! Doctrine 6 http://www.doctrine-project.org
  7. 7. Doctrine 7 http://www.doctrine-project.org
  8. 8. Doctrine 8 http://www.doctrine-project.org
  9. 9. Doctrine 9 http://www.doctrine-project.org
  10. 10. The examples in the next slides use the following models Doctrine 10 http://www.doctrine-project.org
  11. 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. 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. 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. 14. Executing the DQL Query Results Hydrated as Multi-Dimensional Array or Objects Doctrine 14 http://www.doctrine-project.org
  15. 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. 16. Named Queries ‣ Create named queries ‣ Execute named queries ‣ Retrieve named query objects Doctrine 16 http://www.doctrine-project.org
  17. 17. Working with Objects Doctrine 17 http://www.doctrine-project.org
  18. 18. Accessors/Mutators ‣ 3 Different Styles ‣ Easy to use Doctrine 18 http://www.doctrine-project.org
  19. 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. 20. Hydration Modes ‣ As objects ‣ As php arrays ‣ No hydration Doctrine 20 http://www.doctrine-project.org
  21. 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. 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. 23. Working with m2m ‣ Easy to link and unlink ‣ Specify new objects inline ‣ Attach existing objects Doctrine 23 http://www.doctrine-project.org
  24. 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. 25. Deleting ‣ Retrieve and delete ‣ Delete without retrieving ‣ DQL deletes issue individual queries Doctrine 25 http://www.doctrine-project.org
  26. 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
  27. 27. Many2Many Example 1 Doctrine 27 http://www.doctrine-project.org
  28. 28. Many2Many Example 2 Simplified even more Doctrine 28 http://www.doctrine-project.org
  29. 29. Friends List with Equal m2m Doctrine 29 http://www.doctrine-project.org
  30. 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. 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. 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
  33. 33. Core Behaviors ‣ Timestampable ‣ Sluggable ‣ Versionable ‣ I18n ‣ SoftDelete ‣ NestedSet ‣ Geographical Doctrine 33 http://www.doctrine-project.org
  34. 34. Real world example Using the Sluggable and Timestampable behaviors for a BlogPost model Doctrine 34 http://www.doctrine-project.org
  35. 35. The Create Table SQL Doctrine 35 http://www.doctrine-project.org
  36. 36. Behavior In Action Doctrine 36 http://www.doctrine-project.org
  37. 37. The Results Set automatically! Updated! Doctrine 37 http://www.doctrine-project.org
  38. 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. 39. Data Fixtures ‣ Easy to specify m2m data ‣ We did it first, not rails ;) Doctrine 39 http://www.doctrine-project.org
  40. 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. 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. 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. 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. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×