ORM tool or handwritten SQL?

At work we recently had a discussion about whether to use an ORM tool or code the SQL manually. These are discussions like “Apple vs. Nokia” or “Spaces vs. Tabs“. Very emotional.

You should know that tilllate is using a self-made “ORM” tool: Our ORM framework maps table columns to object properties. But it is agnostic of relationships between tables. And when you want to write a complex query, you end up programming long lines of native SQL. Unreadable.

ORM tools increase productivity

I brought up the discussion as we have to make technology decisions for a new project. I know Doctrine 1.2 very well from a spare time project. I am very satisfied with it:

  • No need to think about how the objects are mapped to the database. You just save() them (or persist() them as Doctrine2 calls the function more accurately). You can navigate along the object graph without caring on what exact SQL statements and DB calls are needed.
  • The code is more readable as there is no mix between PHP and SQL
  • Because of the query language is integrated in PHP the IDE can help you with autocompletion.
  • Useful features like data validation or event listeners (“onSave“, “onCreate“) become possible.

Just like back in 1975

The advocates of the “ORM framework” we’re currently using argue that with an ORM tool hiding all SQL from you you lose control of what happens. Also they are worried about performance. Funny, exactly that’s what developers objected 35 years ago against high-level languages like PL/I or APL when they had to switch from assembler. “It does not let you do what you want and it is slow.” 1

As to function, after a learning phase I was able to do whatever I wanted to with it.

As to speed, I believe that the additional abstraction layers certainly cause some performance loss. But for one part the loss is absorbed by built-in caching. And for the rest: With the time you save on development and maintenance you can buy additional hardware.

What do you think?

What is your opinion on ORM tools? Is there a way around them when you do OOP? Are there occasions when writing plain SQL makes sense? Or should we forget about relational databases altogether and switch to NoSQL as jpkoenig suggests?

1. Brooks, Frederick P., Jr (1975). “The Mythical Man-Month”. Addison Wesley,  pp. 135

This entry was posted in Uncategorized. Bookmark the permalink.

One Response to ORM tool or handwritten SQL?

  1. Maarten Manders says:

    I’m a bit biased. 🙂

    “And when you want to write a complex query, you end up programming long lines of native SQL. Unreadable.”

    I beg to differ. Whoever knows to write SQL (and I know you do!) should find this convenient.

    “As to speed, I believe that the additional abstraction layers certainly cause some performance loss.”

    A small performance loss is acceptable. However, when queries become complex, you might end up shooting yourself in the foot without noticing. I think it all depends on how fancy your db queries are.