Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Your Email:
Webmaster Blog

Getting Started with ORM in PHP

by Akash Mehta


ORM, or Object Relational Mapping, is a database design approach that simplifies managing complex databases for programmers. Instead of direct database access, an ORM layer in a PHP framework can make “objects” stored in a database behave like actual objects from a programming perspective - for example, creating a new “car” stored in the database could involve a call to $car->new(). By abstracting actual database access, web development can be more productive and result in more reliable applications. Here’s a quick intro to ORM in PHP.

ORM brings data closer to a programming paradigm, where actual information is stored and accessed through interfaces that resemble actual objects. In PHP frameworks, this is typically achieved through the framework providing classes representing database information, that can then be manipulated as actual objects such as a car or a booking. This is almost always a level above actual database operations - when using an ORM layer, writing actual SQL queries is taken care of, although an understanding of how to is always helpful.

Let’s consider a simple database, consisting of users and posts. ORM defines relationships between objects (and in general, also between tables) where an object either has one or has many of another object. For example, a post might have one author, while an author would have many posts. By adding foreign keys to the actual database, and defining these simple relationships (usually hard-coded), an ORM layer can take care of working with related data.

CakePHP’s ORM layer is possibly one of the most used ORM layers of any PHP framework, and is a good example of how a simple ORM implementation should function. In Cake, when an author model is defined as having many posts, any calls to fetch a particular author will automatically fetch all posts related to that author. The entire database logic is taken care of, and instead of thinking in terms of SQL queries, we get to think in terms of actual objects.

Symfony also has quite a nice ORM layer, although the symfony implementation is a little more hands-off, based around database design conventions and it’s YAML schema file approach. But you don’t need to be using a PHP framework to take advantage of ORM; the Doctrine project provides a powerful ORM layer that can be implemented on its own or within an MVC framework. Doctrine also provides an object-oriented SQL-like language to write database queries - the Doctrine Query Language is is great for complex object retrieval. Propel is another popular option, and is actually the ORM layer behind Symfony.

At the end of the day, ORM provides a fundamental advantage over writing raw SQL queries: it’s designed to retrieve information, not record sets. While thinking about data in terms of objects is a major paradigm shift from hands-on PHP scripting, it’s a great way to deal with the challenge of complex databases. So what are you waiting for? Grab a framework or a library, and dive in to the world of ORM.




Related Posts
» jQuery: Interaction Design for PHP Developers
» Web Hosting: Leave it to the Professionals
» Running background processes in PHP
» 7 Useful Twitter Tools for business
» Debugging PHP with Firebug and FirePHP
 


Leave a Reply