Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Doctrine ORM, why is DBAL needed in addition to PDO?

I've been working with Doctrine 2 ORM for some time, and there's something I've never quite understood.

What purpose does the Doctrine DBAL (database abstraction layer) serve? PDO itself a database abstraction layer, so why can't the ORM work directly with PDO?

I'm not trying to find a way around using DBAL or anything. I've just never understood why the extra layer is needed, and can't seem to find a clear answer in the documentation.

like image 641
cantera Avatar asked Dec 21 '22 02:12

cantera


2 Answers

No, PDO is a "data-access layer", not a "database abstraction layer". Meaning that you can switch databases and still make the same method calls, but PDO will not re-write sql queries to match the selected database or emulate any database functionality.

Per PHP PDO docs:

PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.

like image 88
Mike Purcell Avatar answered Jan 13 '23 13:01

Mike Purcell


Doctrine2 actually supports some non-PDO databases so that is one reason. It's also useful to look at the source code. The Connection class (for example) has a nice:

public function insert($tableName, array $data)

Which inserts a new record complete with escaping.

like image 31
Cerad Avatar answered Jan 13 '23 13:01

Cerad