Doctrine Php Object Relational Mapper
-
Upload
guesta3af58 -
Category
Technology
-
view
8.534 -
download
2
description
Transcript of Doctrine Php Object Relational Mapper
![Page 1: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/1.jpg)
Doctrine http://www.doctrine-project.org
Doctrine, PHP Object Relational Mapper
1
![Page 2: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/2.jpg)
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
![Page 3: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/3.jpg)
Doctrine http://www.doctrine-project.org
The Layers
3
![Page 4: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/4.jpg)
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
![Page 5: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/5.jpg)
Doctrine http://www.doctrine-project.org
Will it solve world hunger?
‣ No
‣Will not solve all your problems
‣ Helps more than it hurts
5
![Page 6: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/6.jpg)
Doctrine http://www.doctrine-project.org
1, 2, 3, Let’s go!
6
![Page 7: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/7.jpg)
Doctrine http://www.doctrine-project.org7
![Page 8: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/8.jpg)
Doctrine http://www.doctrine-project.org8
![Page 9: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/9.jpg)
Doctrine http://www.doctrine-project.org9
![Page 10: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/10.jpg)
Doctrine http://www.doctrine-project.org
The examples in the next slides use the following models
10
![Page 11: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/11.jpg)
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
![Page 12: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/12.jpg)
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
![Page 13: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/13.jpg)
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
![Page 14: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/14.jpg)
Doctrine http://www.doctrine-project.org
Executing the DQL
14
Query ResultsHydrated as
Multi-Dimensional Array or Objects
![Page 15: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/15.jpg)
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.
![Page 16: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/16.jpg)
Doctrine http://www.doctrine-project.org16
Named Queries‣ Create named queries
‣ Execute named queries
‣ Retrieve named query objects
![Page 17: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/17.jpg)
Doctrine http://www.doctrine-project.org
Working with Objects
17
![Page 18: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/18.jpg)
Doctrine http://www.doctrine-project.org18
Accessors/Mutators
‣ 3 Different Styles
‣ Easy to use
![Page 19: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/19.jpg)
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
![Page 20: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/20.jpg)
Doctrine http://www.doctrine-project.org20
Hydration Modes‣ As objects
‣ As php arrays
‣ No hydration
![Page 21: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/21.jpg)
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
![Page 22: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/22.jpg)
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
![Page 23: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/23.jpg)
Doctrine http://www.doctrine-project.org23
Working with m2m
‣ Easy to link and unlink
‣ Specify new objects inline
‣ Attach existing objects
![Page 24: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/24.jpg)
Doctrine http://www.doctrine-project.org24
Updating
‣ Retrieve and update
‣ Update with one DQL query
‣ DQL updates don’t issue events/hooks for updating
![Page 25: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/25.jpg)
Doctrine http://www.doctrine-project.org25
Deleting
‣ Retrieve and delete
‣ Delete without retrieving
‣ DQL deletes issue individual queries
![Page 26: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/26.jpg)
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
![Page 27: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/27.jpg)
Doctrine http://www.doctrine-project.org27
Many2Many Example 1
![Page 28: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/28.jpg)
Doctrine http://www.doctrine-project.org28
Many2Many Example 2Simplified even more
![Page 29: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/29.jpg)
Doctrine http://www.doctrine-project.org29
Friends List with Equal m2m
![Page 30: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/30.jpg)
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
![Page 31: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/31.jpg)
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!
![Page 32: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/32.jpg)
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
![Page 33: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/33.jpg)
Doctrine http://www.doctrine-project.org
Core Behaviors
‣ Timestampable‣ Sluggable‣ Versionable‣ I18n‣ SoftDelete‣ NestedSet‣ Geographical
33
![Page 34: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/34.jpg)
Doctrine http://www.doctrine-project.org
Real world exampleUsing the Sluggable and Timestampable behaviors for a BlogPost model
34
![Page 35: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/35.jpg)
Doctrine http://www.doctrine-project.org
The Create Table SQL
35
![Page 36: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/36.jpg)
Doctrine http://www.doctrine-project.org
Behavior In Action
36
![Page 37: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/37.jpg)
Doctrine http://www.doctrine-project.org
The Results
37
Set automatically!
Updated!
![Page 38: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/38.jpg)
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
![Page 39: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/39.jpg)
Doctrine http://www.doctrine-project.org39
Data Fixtures
‣ Easy to specify m2m data
‣ We did it first, not rails ;)
![Page 40: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/40.jpg)
Doctrine http://www.doctrine-project.org40
Data Fixtures Inline
‣ Specify data fixtures inline
‣ More readable
‣ If a relationship exists, you can populate it inline
![Page 41: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/41.jpg)
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.
![Page 42: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/42.jpg)
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
![Page 43: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/43.jpg)
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
![Page 44: Doctrine Php Object Relational Mapper](https://reader031.fdocuments.us/reader031/viewer/2022020105/54b320884a7959a4688b45a1/html5/thumbnails/44.jpg)
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