Introduction to Object Relational Mapping (ORM) Tools

From GeeklogWiki
Revision as of 21:14, 25 October 2005 by Joelwan (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Object Relational Mapping (ORM) Tools provide a slick way of persisting objects (data) to a database. I personally don't know much about the history of ORM tools but I will vouch for the power they bring to a project. ORM tools are not for detailed oriented programmers that have to know all the internels of how things work. A good ORM implementation should be black-boxed so that once you understand what they provide you with you should not care so much how it works...only that it does. I won't's a leap of faith that was even hard for me to make.

So what do ORM tools do? Different ORM tools do different things but generally you can expect the following.

  1. ORM tools are made database aware through use of some database abstraction layer...typically outside the scope of the ORM tool (i.e. JDBC, ODBC or in PHP PEAR::DB).
  2. ORM tools can produce pure PHP model classes where you generally have one model object per database table.
  3. ORM tools can produce database schema's for the various database management systems supported
  4. ORM tools greatly reduce the need the developers having to write SQL. Why in the world would you want that? Well, developers can spend an inordinate amount of time getting a SQL statement right within their code. The resulting SQL has no gaurantee that it will adhere to SQL standards so that it can be used across DBMSs. A good example of such things is the Geeklog 1.3.x where we have the REPLACE INTO statements which are specific to MySQL. The other huge benefit is by reducing the need to write SQL, the developer can concentrate on innovation. How does this work, you ask. Well, in the example of Propel, if you have, let's say, a story save it you simply issue $myStoryObj->save(). Similar methods exist for retrieval and deletion.
  5. ORM tools honor complex relationships. Thus if you have an object that has child objects on it (i.e. a customer who can have many addresses) when you issue a save on the parent, the ORM tool is smart enough to save all objects and will ensure they are wrapped in a transaction.

I was first exposed to ORM tools through an associate at work who was learning all about [Hibernate]. Hibernate is java-based and it was a good fit as an alternative to EJB's in our [IBM Websphere Environment]. After being exposed to Hibernate, I was immediately asking whether or not such a tool existed for PHP when I stumbled onto [Propel]. Now that you know a tad bit more about them, let's talk about using Propel.

ORM Tools

DB Visual Architect - An Object-Relational Mapping (ORM) tool acting as a bridge between object model, data model and relational database

Php Object Generator - Generates free, tested ORM code that you can use for your PHP4/PHP5 application.