Top Banner
Tetraboy asks: “Hey Maggie, do you have any bears?”
74

Tetraboy asks: “Hey Maggie, do you have any bears?”maggienelson.com/conferences/phptek2009/ORM_in_the_PHP_Worl… · php|tek 2009. About Me

Aug 19, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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

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

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
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

“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

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

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

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

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

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

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

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

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

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

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

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

(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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Thanks!

maggienelson.com