![Page 1: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/1.jpg)
Tetraboy asks: “Hey Maggie, do you have any bears?”
![Page 2: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/2.jpg)
No, but here’s a slow loris!
![Page 3: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/3.jpg)
![Page 4: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/4.jpg)
ORM in the PHP World
Maggie Nelsonphp|tek 2009
![Page 5: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/5.jpg)
About Me
<3 databases
<3 PHP
</3 how PHP and database play together
![Page 6: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/6.jpg)
What is this “ORM” you speak of?
![Page 7: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/7.jpg)
Objects
- Represent things in real life
- Have properties
- Have methods that access properties in some way
- Inherit from each other
properties organized as objects
![Page 8: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/8.jpg)
Relations
- Represent how things in real life connect to each other
- Also represent how properties of things connect to each other
properties organized as relations
![Page 9: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/9.jpg)
Map
properties organized as objects
properties organized as relations
![Page 10: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/10.jpg)
Encapsulate this!
![Page 11: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/11.jpg)
Encapsulation
- Hide the pesky details (“how does the car work again?”)
- Provide a common interface (“here’s the steering wheel, here’s the gas pedal”)
![Page 12: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/12.jpg)
Objects
Public methods and properties provide a natural interface.
Restricted methods and properties hide away the details.
![Page 13: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/13.jpg)
SQL is a type of interface
![Page 14: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/14.jpg)
SQL as an interface
SQL provides public access to stored data and way to combine the data.
Database internals are completely hidden away.
![Page 15: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/15.jpg)
Objects TablesSQL
![Page 16: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/16.jpg)
Oh, man, but SQL seems pretty detailed! We should totally hide it!
![Page 17: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/17.jpg)
I agree, more encapsulation is needed, but...
Let’s talk about PHP!
![Page 18: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/18.jpg)
PHP is great because it is...
easy
fast
intuitivelight-weight
well documented
etc.
![Page 19: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/19.jpg)
Objects aren’t arrays
ORM (Object Relational Mapping)
not ARM (Array Relational Mapping?)
![Page 20: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/20.jpg)
“The Database Class” != ORM
“The Database Class” and the persistence objects should not live in the same spot
![Page 21: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/21.jpg)
The “Database Class”
Deals with database access.
“Hey, MySQL, execute this query, please, here are my credentials.”
![Page 22: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/22.jpg)
ORM
Persists application data.
$kitten->save();
![Page 23: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/23.jpg)
PHP is already a language
I hope this ORM won’t make me learn all new syntax and stuff!
![Page 24: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/24.jpg)
Let’s get some ORM on!
![Page 25: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/25.jpg)
In the ideal world...
Object (can) persist.
Object don’t know HOW and WHERE they persist.
![Page 26: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/26.jpg)
CRUD!
![Page 27: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/27.jpg)
Persisting objects via CRUD
C - create (add)
R - read (select)
U - update (modify)
D - delete (remove)
![Page 28: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/28.jpg)
ORM Patterns
![Page 29: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/29.jpg)
Table Data Gateway usually paired with Row Data Gateway
An object that acts as a gateway to a single row / table.
There is one instance per row / table.
![Page 30: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/30.jpg)
Active Record (think Ruby on Rails!)
![Page 31: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/31.jpg)
Data Mapper (my fave!)
![Page 32: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/32.jpg)
(Thanks to Martin Fowler for the delicious pattern diagrams!)
![Page 33: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/33.jpg)
Generating SQL
![Page 34: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/34.jpg)
what does this mean?
![Page 35: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/35.jpg)
this may be clearer...
![Page 36: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/36.jpg)
but this is nicer (skoop agrees)
![Page 37: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/37.jpg)
compare...
![Page 38: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/38.jpg)
Benefits of ORM
Gets rid of repetitive code.
Avoids having to set up database objects “by hand”.
Easy to understand for new developers.
Portability*.
![Page 39: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/39.jpg)
Downsides of ORM
Gets really convoluted when retrieving complicated relationships.
Takes away the power of optimizing database access.
May be inefficient.
Strong MySQL preference in the PHP world.
![Page 40: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/40.jpg)
Ok, so what’s out there?
![Page 41: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/41.jpg)
Propel
“It allows you to access your database using a set of objects, providing a simple API for storing and retrieving data.”
Provides then M in MVC.
Propel uses the Creole database abstraction layer.
“ORM tool but also a code generator.”
Row Data Gateway + Table Data Gateway (NOT Active Record!)
![Page 42: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/42.jpg)
Propel
Object classes for rows
Peer classes for table operations
Build database in XML
Criterion object for more complex queries.
![Page 43: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/43.jpg)
Criterion object
$c = new Criteria();$c->add(SomePeer::ID = 10); $results = SomePeer::doSelect($c);
![Page 44: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/44.jpg)
Doctrine
proprietary object oriented SQL dialect called Doctrine Query Language (DQL), inspired by Hibernates HQL.
![Page 45: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/45.jpg)
DQL simple example
$things = $this->getThing() ->orderBy('created_at DESC') ->limit(50) ->execute();
![Page 46: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/46.jpg)
DQL complicated example
static function sort2dBySubLength($arr, $key, $order){ foreach($arr as $sub) $sc[] = count($sub[$key]); $order = $order == 'desc' ? SORT_DESC : SORT_ASC; array_multisort($sc, SORT_NUMERIC, $order, $arr); return $arr; }
![Page 47: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/47.jpg)
dORM
by Olivier Lalonde
“It implements multiple software design patterns such as Unit of Work, Identity Map, Lazy Load, Foreign Key Mapping, Association Table Mapping, Metadata Mapping and Data Mapper.”
http://getdorm.com/
![Page 48: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/48.jpg)
Easy to use and understand
$dorm->getClassName("id");$dorm->save($object);$dorm->delete($object);
![Page 49: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/49.jpg)
DORM Example: XML Mapping
<dorm> <map database="mysql://user:pass@hostname/dbname"> <!-- sample class --> <className table="tableName">
<!-- sample scalar property --> <propertyName1 field="fieldName" setter="setterMethod" getter="getterMethod" />
<!-- sample object property --> <propertyName2 fkey="fkeyName" class="objectClassName" setter="setterMethod" getter="getterMethod" />
<!-- sample object array property -->
<propertyName3 pivot="pivotTableName" class="objectClassName" key="arrayKeyFieldName" />
</className> </map></dorm>
![Page 50: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/50.jpg)
Zend Framework
Zend_Db component:
Non-ORM related parts:- Database Adapters- Query profiler
ORM parts:- Query builder- CRUD functions- Table and Row OO patterns- Zend_Db_Mapper (proposed)
![Page 51: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/51.jpg)
ZF Example 1: Running custom queries
$stmt = $db->query( 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?', array('goofy', 'FIXED'));
![Page 52: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/52.jpg)
ZF Example 2: ORM as an SQL “Translator”
$data = array( 'updated_on' => '2007-03-23', 'bug_status' => 'FIXED');
$n = $db->update('bugs', $data, 'bug_id = 2');
![Page 53: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/53.jpg)
ZF Example 3: Zend_Db_Mapper
public function increaseNumberOfBeds($byCount) { for($i = 0; $i < $byCount; $i++) { $bed = new Clinic_Bed(); $bed->setStation($this); $this->beds->add($bed); } return $this->getBeds(); }
![Page 54: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/54.jpg)
Xyster Framework
Based on ZF, but takes some and improves on a lot.
ORM based on a Data Mapper design pattern.
http://xyster.devweblog.org/wiki/xyster/Orm
![Page 55: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/55.jpg)
CodeIgniter
ActiveRecord
Database class that manages data retrieval and parsing.
Nice and light - doesn’t really impose anything on the developer.
![Page 56: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/56.jpg)
CodeIgniter Example 1: Running custom queries
$this->db->query('YOUR QUERY HERE');
![Page 57: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/57.jpg)
CodeIgniter Example 2: result_object()
$query = $this->db->query("YOUR QUERY");
if ($query->num_rows() > 0){ foreach ($query->result() as $row) { echo $row->title; echo $row->name; echo $row->body; }}
![Page 58: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/58.jpg)
eZ Component
Provides Persistent Object which provides Persistent Mapping - you can customize how an object is represented in the database.
![Page 59: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/59.jpg)
eZ Component example
1. <?php 2. $def = new ezcPersistentObjectDefinition(); 3. $def->table = "persons"; 4. $def->class = "Person"; 5. 6. $def->idProperty = new ezcPersistentObjectIdProperty; 7. $def->idProperty->columnName = 'id'; 8. $def->idProperty->propertyName = 'id'; 9. $def->idProperty->generator = new ezcPersistentGeneratorDefinition( 'ezcPersistentNativeGenerator' ); 10. 11. $def->properties['name'] = new ezcPersistentObjectProperty; 12. $def->properties['name']->columnName = 'full_name'; 13. $def->properties['name']->propertyName = 'name'; 14. $def->properties['name']->propertyType = ezcPersistentObjectProperty::PHP_TYPE_STRING; 15. 16. $def->properties['age'] = new ezcPersistentObjectProperty; 17. $def->properties['age']->columnName = 'age'; 18. $def->properties['age']->propertyName = 'age'; 19. $def->properties['age']->propertyType = ezcPersistentObjectProperty::PHP_TYPE_INT; 20. 21. return $def; 22. ?>
![Page 60: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/60.jpg)
symfony
Offers support for both Propel and Doctrine.
![Page 61: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/61.jpg)
PHPLinq
SQL translator
http://phplinq.codeplex.com/
![Page 62: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/62.jpg)
PHPLinq example
$result = from('$employee')->in($employees) ->where('$employee => strlen($employee->Name) == 4') ->orderBy('$employee => $employee->Name') ->thenByDescending('$employee => $employee->Age') ->select('new { "EmailAddress" => $employee->Email, "Domain" => substr($employee->Email, strpos($employee->Email, "@") + 1) }');
![Page 63: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/63.jpg)
Solar Framework
Solar_Sql_Model (for dealing wtih models) - uses Table_Data_Gateway- returns Collection of Object
Solar_Sql (for SQL generation and handling results)
http://solarphp.com/package/Solar_Sql_Model
![Page 64: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/64.jpg)
Outlet PHP ORM
SQL translator
http://www.outlet-orm.org
![Page 65: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/65.jpg)
Outlet PHP Orm Example: SQL + property mix
$outlet = Outlet::getInstance(); // select using prepared statement$projects = $outlet->select( 'Project', 'WHERE StatusID = ? OR StatusID = ?', array(0, 1)); // select one entity by primary key$project = $outlet->load('Project', 1); // select using a relationship method $bugs = $project->getBugs('WHERE {Bug.StatusID} = 1'); // select using fluent-interface$bugs = $outlet->from('Bug b') ->with('Project') ->where('{b.Status} = ?', array(Bug::OPEN)) ->find();
![Page 66: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/66.jpg)
CoughPHP
Collection Handling Framework
Very light weight, very simple.
http://www.coughphp.com
![Page 67: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/67.jpg)
CoughPHP Example: CRUD
Create (INSERT)
$customer = new Customer();$customer->setName('First Customer');$customer->save();$customerId = $customer->getKeyId();
Read/Retrieve (SELECT)
$customer = Customer::constructByKey($customerId);
Update (UPDATE)
$customer->setName('New Name');$customer->save();
Delete/Destroy (DELETE)
$customer->delete();
![Page 68: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/68.jpg)
ORMer
ActiveRecord
http://www.greaterscope.net/projects/ORMer
![Page 69: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/69.jpg)
ORMer Example
// Pull user objects "where email='[email protected]'"
$users = user::find()->where('email=?')->parameters('[email protected]');
![Page 70: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/70.jpg)
And many more?
![Page 71: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/71.jpg)
Alternatives?
![Page 72: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/72.jpg)
Non-relational data storage
- key-value databases
- resource-oriented interfaces (think a REST-ful API)
- *QL (Facebook’s FQL, Google’s GQL, etc.)
![Page 73: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/73.jpg)
More layers!
Objects TablesSQL ViewsCache*QLAPI
![Page 74: Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me](https://reader036.vdocument.in/reader036/viewer/2022071007/5fc4a5761b72e83091547f2e/html5/thumbnails/74.jpg)
Thanks!
maggienelson.com