KillerPHP Blog

What are ORM Frameworks?

December 8, 2009

Database Image

In the following article we will learn a few things about ORM frameworks:

  • What they are.
  • What they do.
  • When and why to use them.
  • And finally, what ORM options PHP’ers have.

Let’s start …

If you’re working with PHP, you will certainly find yourself working with relational databases (like MySQL) sooner or later. Anyone who has ever built a reasonably complex web application knows, that the SQL can get really hairy at times – especially when you consider all the data filtering that you have to deal with!

ORM frameworks to the rescue!

ORM is yet another nerd-acronym, it is short for Object Relational Mapping. In a nutshell, an ORM framework is written in an object oriented language (like PHP, Java, C# etc…) and it is designed to virtually wrap around a relational database. If you look at the name (ORM), it basically translates into: mapping objects to relational tables.

There are many different competing ORM frameworks out there, because many nerds have their own ideas as to how to best do things – nothing new here! But you know what, some of these nerds are right … not all ORM frameworks are created equal.

orm-framework

OK, but what does an ORM framework do?

Basically, the ORM framework/software generates objects (as in OOP) that virtually map (like the map of a city) the tables in a database. Then you as a programmer, would use these objects to interact with the database. So the main idea, is to try and shield the programmer from having to write optimized SQL code – the ORM generated objects take care of that for you. So let’s take a look at a simple example:

Say for instance you had a database with two tables:

  • Clients
  • Products

With a little bit of configuration on your part, the ORM framework would then create corresponding objects (say, clients_object and products_object) that would handle all the database interaction. So let’s say you need to add a new client to the database, you would just have to use the ORM’s clients_object to add the new client.

For example, it could be as simple as calling the object’s ‘save()’ method:

client = new clients_object("Stefan","Mischook");
client.save();

The above of course, is just pseudo code, mainly because the syntax will vary from ORM framework and from language to language. But hopefully you get the general idea of how much easier an ORM framework can make things (no SQL!) .. not to mention how much cleaner your application’s code will be.

Some other advantages of using ORM frameworks

1. Harmonization of data types between the OO language (in our case, PHP) and the SQL database. All relational databases use data types for each of the fields, for example: int, small int, blob, char etc. The thing is, that sometimes you have to convert the data types on the fly to properly add a record to the database. A good ORM will take care of these details for you.

2. Using an ORM will create a consistent code base for your application since much (if not all) of the code used to interact with the database will be PHP – no SQL code to mess things up. This makes it easier to write and debug your application, especially if you have more programmers on a job.

3. ORM frameworks will shield your application from SQL injection attacks since the framework will be filtering the data for you.

4. Database Abstraction; I am a little hesitant to make this point because in practice, over the last 10-15 years, I have only seen it once where we switched databases on an application. That said, ORM will make this much easier since it takes care of writing all the SQL code, data type conversions etc …

When to use an ORM framework?

From my personal experience, an ORM framework becomes more useful as the size and complexity of the project increases. If you just have a simple database with say 5 tables and 5-6 queries … setting up an ORM framework may be overkill. I would start considering the use of ORM when:

  • You have 3 or more programmers on a web application.
  • Your database consist of 10+ tables.
  • You have say 10+ queries to make.

ORM frameworks can’t do it all

If you think that using an ORM framework will allow you to forget SQL and never have to look back, think again. Once you jump into the ORM world, you will find that about 80-90% of your queries can be handled by the ORM generated objects. It is inevitable that at some point you will need to drop down and use some SQL or some SQL like query language.

In fact, ORM frameworks often have their own *QL query language that looks a lot like SQL. Doctrine, a popular PHP based ORM framework has DQL (Doctrine Query Language) and the very popular Hibernate (used in the Java and .Net world) has HQL. Going even further, Hybernate allows you to write straight SQL if need be.

Despite the need for a SQL like language in ORM frameworks, they can still be very valuable tools in your PHP work.

ORM Frameworks for PHP programmers

Not an exhaustive list, but here are a few ORM frameworks to consider:

Thanks for reading,

Stefan Mischook
www.killerphp.com