• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Object Relational Mapping in PHP
 

Object Relational Mapping in PHP

on

  • 33,884 views

"Object Relational Mapping in PHP"

"Object Relational Mapping in PHP"

Presentation given to PHPNW on 05/05/09 by Rob Knight

Statistics

Views

Total Views
33,884
Views on SlideShare
33,232
Embed Views
652

Actions

Likes
26
Downloads
632
Comments
5

12 Embeds 652

http://robknight.org.uk 405
http://disole.wordpress.com 88
http://www.slideshare.net 59
http://www.linkedin.com 39
http://www.php.rk.edu.pl 22
http://mvc-in-php.blogspot.com 16
http://mvc-in-php.blogspot.ru 6
http://translate.googleusercontent.com 6
http://blog.ignar.name 5
http://ignar.name 4
http://dev.robknight.org.uk 1
https://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

15 of 5 previous next Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • very good, thanks!
    Are you sure you want to
    Your message goes here
    Processing…
  • .key is the mac application keynote (similar to ms powerpoint)
    Are you sure you want to
    Your message goes here
    Processing…
  • downloaded file showing extention .key cann't open. May i get help from any one how to open it?
    Are you sure you want to
    Your message goes here
    Processing…
  • Como descargar la presentacion , ya que cuando doy click en ’download’ me descarga un archivo con extension.key
    Are you sure you want to
    Your message goes here
    Processing…
  • Como descargar la presentacion , ya que cuando doy click en 'download' me descarga un archivo con extension.key
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Object Relational Mapping in PHP Object Relational Mapping in PHP Presentation Transcript

  • Object-Relational Mapping in PHP PHPNW May 2009 Rob Knight - Lead Technical Architect, PRWD http://robknight.org.uk
  • What is ORM? • Manages the translation of objects into relational databases, and vice-versa • Most modern programming languages, including PHP, have a concept of objects • Most databases, including MySQL, Oracle and Postgresql are relational
  • Objects • Classes define the blueprint • Classes can inherit from each other • Objects contain data and methods • Object data can include other objects and arrays/lists
  • A very simple class <?php class Employee extends Person { protected $salary; // floating-point value protected $contractLength; // integer value protected $manager; // reference to another Employee object protected $skills; // array of 'Skill' objects protected $history; // array of 'History' objects protected $id; // unique identifier }
  • Relational Databases • Basic unit of storage is the table • Tables can only contain simple data types (numbers, strings, binary data) • No concept of arrays or lists • Tables can be related by foreign keys
  • A simple table id name salary manager_id contract_length 1 Alice 20000.00 2 12 2 Bob 150000.00 NULL NULL 3 Clive 15000.00 5 6 4 Derek 32000.00 2 NULL 5 Edith 45000.00 2 NULL
  • Advantages of ORM • Everything in PHP • In theory, no need to understand the underlying data storage system • ORM can make it easy to adopt good patterns and good database design • Enables familiar OO concepts for manipulating data
  • Use of PHP concepts <?php $employee = new Employee(); // use of object-oriented concepts $employee->setName(‘Joe’); $employee->save(); // underlying database could by MySQL, SQLite, Postgres or Oracle $peopleToFire = EmployeeTable::select(‘id’)->where(‘salary < 20000’) ->andWhere(‘relatedToChairman = 0’)->limit(5); $subordinates = EmployeeTable::select(‘*’)->where(‘manager_id = ?’, $manager->id); // automatic variable escaping ?>
  • Disadvantages of ORM • The extra layer of abstraction can be a performance hit • Can be verbose • OO isn’t always best • Can be inflexible • Sometimes framework-dependent
  • ORM can be verbose Using Propel $c = new Criteria(); $cton1 = $c->getNewCriterion(ArticlePeer::TITLE, '%FooBar%', Criteria::LIKE); $cton1 = $c->getNewCriterion(ArticlePeer::SUMMARY, '%FooBar%', Criteria::LIKE); $cton1->addOr($cton2); $c->add($cton1); $c->add(ArticlePeer::PUBLISHED_AT, $begin, Criteria::GREATER_THAN); $c->addAnd(ArticlePeer::PUBLISHED_AT, $end, Criteria::LESS_THAN); $article = ArticlePeer::doSelect($c); SQL Query SELECT * FROM article WHERE (title LIKE ‘%FooBar%’ OR summary LIKE ‘%FooBar’) AND published_at > {$begin} AND published_at < {$end}
  • There are many ORMs • Doctrine <http://www.doctrine-project.org> • Propel<http://propel.phpdb.org> • Zend_Db <http://framework.zend.com/manual/en/zend.db.html> • Kohana ORM <http://docs.kohanaphp.com/libraries/orm> • PDO <http://www.php.net/pdo>
  • ORM Patterns From ‘Patterns of Enterprise Application Architecture’ - Martin Fowler, 2003 • Table Data Gateway Zend_Db_Table, Propel ‘Peer’ classes • Row Data Gateway Zend_Db_Table_Row • Data Mapper • Active Record Doctrine_Record, Propel classes
  • Table Data Gateway • One class per table • Basic functions for inserting, updating, deleting data from the table • Can also handle related tables • Data is returned in simple array/POPO form
  • An example <?php // Using Zend_Db_Table to fetch a record from a table class BlogPostTable extends Zend_Db_Table_Abstract { ... } $table = new BlogPostTable(); $select = $table->select()->where(‘author_id = ?’, $id)->order(‘created DESC’); $row = $table->fetchRow($select); // row is an array, with keys for column names echo $row->title; // prints the title of the blog post ?>
  • Row Data Gateway • Class for manipulating a single row • Separates finding/searching from manipulation • Database access logic only
  • An example <?php // create row using table object $table = new BlogPostTable(); $newRow = $table->createRow(); // set values $newRow->title = ‘New Blog Post’; $newRow->body = ‘Lorem ipsum....’; // save the row to the table $newRow->save(); ?>
  • Active Record • Similar to Row Data Gateway • Classes inherit from a ‘Record’ class • Classes may also implement ‘behaviours’ • Some mixing of data storage code and domain logic
  • An example <?php class User extends Doctrine_Record { public function setTableDefinition() { $this->hasColumn('username', 'string', 255); $this->hasColumn('password', 'string', 255); } public function authenticate($username, $password) { return ($this->username == $username) && ($this->password == $password); } } $table = Doctrine::getTable(‘User’); $user = $table->find($id); if ($user->authenticate($username, $password)) { print “Authenticated as user id {$id}”; }
  • Data Mapper • Takes your object model and saves it to database • Mapper is external to your classes • Good separation of concerns, keeps the OO away from the data • Hard to implement as a ‘drop-in’ system
  • Extras • Schema management • Auto-creation of forms, admin areas • Database creation and migration • Plugins and behaviours
  • Schemas User: tableName: users columns: id: type: integer(4) primary: true autoincrement: true unsigned: true forename: type: string(50) surname: type: string(50) email: type: string(50) password: type: string(32) actAs: Timestampable: created: name: created type: integer options: unsigned: true
  • Plugins • Logging • Profiling • Behaviours - timestampable, sluggable and more • Events • Inheritance • Caching
  • Performance Issues • Joins Complex relationships between objects can lead to inefficient joins between tables • ‘Hydration’ Transforming database rows into full PHP objects can be expensive • Many classes, many files Loading many auto-generated classes can cause performance issues
  • Alternatives • Object databases • Document databases • Key-value stores • Hybrids
  • Object and Document Databases • Persevere Stores JavaScript/JSON objects • CouchDB Stores documents in JSON format • Abdera Stores documents using the Atom Publishing Protocol
  • Key-value stores • SimpleDB • BigTable • Mnesia
  • JSON: a better fit? employees: [ { name: “Alice”, salary: 35000.00, skills: [ “C++”, “PHP”, “SQL” ] }, { name: “Bob”, salary: 24000.00, skills: [ “Java”, “C#” ] } ] Might JSON object storage be better for PHP?
  • Conclusions • Choose your ORM library carefully • Experiment • Don’t over-complicate • Maybe ORM is the wrong choice anyway? • Alternatives are starting to become viable
  • Thanks for listening Any questions? Slides available at http://robknight.org.uk/presentations