A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

An example of an Object-relational Mapper in PHP

Dennis Hotson posted in his blog a brief and good example of an ORM for PHP.

What is an Object-relational Mapper?

Data management tasks in object-oriented (OO) programming are typically implemented by manipulating objects that are almost always non-scalar values. For example, consider an address book entry that represents a single person along with zero or more phone numbers and zero or more addresses. This could be modeled in an object-oriented implementation by a “person object” with “slots” to hold the data that comprise the entry: the person’s name, a list of phone numbers, and a list of addresses. The list of phone numbers would itself contain “phone number objects” and so on. The address book entry is treated as a single value by the programming language (it can be referenced by a single variable, for instance). Various methods can be associated with the object, such as a method to return the preferred phone number, the home address, and so on.

However, many popular database products such as structured query language database management systems (SQL DBMS) can only store and manipulate scalar values such as integers and strings organized within normalized tables. The programmer must either convert the object values into groups of simpler values for storage in the database (and convert them back upon retrieval), or only use simple scalar values within the program. Object-relational mapping is used to implement the first approach.

The heart of the problem is translating those objects to forms that can be stored in the database for easy retrieval, while preserving the properties of the objects and their relationships; these objects are then said to be persistent.


The full source code can be found in Github.

I’m sure this will come in handy as a reference in the future. Thanks to Dennis Hotson for sharing.

This entry was posted in General. Bookmark the permalink.

3 Responses to An example of an Object-relational Mapper in PHP

  1. Jonathon says:

    Reinventing the wheel? There are already some good/great ORM’s in PHP. I’ve been working with Doctrine in a couple of projects, but am really waiting for Doctrine 2.0 before putting too much effort into a production product.

  2. Wenbert says:

    I am also waiting for Doctrine 2.0 too. I saw the slides for Doctrine2 and it looks very impresive.

  3. Thanks for the comments guys.

    Yep, it’s reinventing the wheel. Writing a little ORM is mostly just a good way to spend an afternoon. ;-)

    If you’ve ever tried to use doctrine, you’d notice that it’s pretty cpu and memory intensive. I think there’s room for a lightweight option.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>