Are DB layers like Zend_Db making people forget SQL?


While possibly a great help and a way to speed development up, are database access classes like Zend_Db and others making people forget about SQL?

ActiveRecord, Table Row Gateway and whatnot, are people trying to constrain their thinking inside the bounds given by these implementations?

You see a lot of people trying to find answers to simple questions like “How to select x by y from table” which are dead simple to perform by writing SQL. But no, they will not write SQL. They want to find out how to perform it in their DB class of choice and get stuck in that.

Another thing people don’t realize is that writing the same “fluent” query, such as

$db->select('foo')->from('bar')->where('foo = bar');

is just the same as sprinkling SQL query strings everywhere.

Not writing SQL will not magically stop you from doing stupid things. Even more, not writing SQL might enable you to do stupid things.

Ever heard about performance? Doing some thing with a DB class might sneak up on you and end up doing useless queries. When you could use a single query to all the data you will need, using these abstractions might end up using more queries and adding a pointless performance drop.

I’m not saying “don’t use DB classes like these”. My opinion is somewhere in between “use” and “don’t use”.

Don’t use them unless you know what you are doing: If you don’t know how many queries you’re performing, you might be in for a surprise. Of course this doesn’t always matter much, but it’s good to know what’s going on behind the scenes nevertheless. Also, if you can’t do some query with your library of choice, don’t get stuck – You have SQL. You don’t know how to do it with SQL either? Well, it is about time to learn it then.

I personally don’t like interfaces which make you mix SQL and code in a weird way, but I’m a big fan of Propel. Propel, in my opinion, makes it incredibly simple to perform some tasks, and it can even pull out JOINs that would be a pain to parse by hand from an SQL resultset.

Not to mention, that Propel’s get* methods have their return types defined with phpDoc syntax, so if you are using something like Zend Neon, you will get code assist on the model’s variables. Nice!

ORM is nice, but don’t forget your old buddy SQL.