Posts

Doctrine DQL or SQL

Developers that use Doctrine are often puzzled on what should they use for SELECT queries - DQL or SQL? Short answer: both. Long answer: Official Doctrine documentation here says: A common mistake for beginners is to mistake DQL for being just some form of SQL and therefore trying to use table names and column names or join arbitrary tables together in a query. You need to think about DQL as a query language for your object model, not for your relational schema. That page clearly tells us: don't use DQL to built complicated and heavy queries. But what about database independent code? Query building? You might ask... For former question Doctrine has whole  DBAL layer located in  Doctrine\DBAL namespace, for latter it has  Doctrine\DBAL\Query\QueryBuilder which builds pure SQL code in similar way as  Doctrine\ORM\QueryBuilder builds DQL and A LOT! of developers don't even aware of its existence! $qb = $this -> connection ->createQueryBuilder(); $qb ->from(