问题
What is the benefit of use DQL in 'select' statement in doctrine vs native sql?
e.g
$qb = $em->getRepository('repositoryname')->createQueryBuilder('r);
$qb->someDqlCondition();
$qb->getQuery()->getResult();
vs
$sql = 'string with SELECT sql statement'
$query = $em->->createQuery($sql)
$query->getResult();
I discuss about it with my colleague and he say that native sql is better. I think that dql give us ability to change database driver.Is there any other benefit of using dql? What about performance?
回答1:
Doctrine Query Language (DQL) is like an abstraction of your queries. This makes queries independent of the version or type of (SQL) database that your project is built on.
From the documentation:
You need to think about DQL as a query language for your object model, not for your relational schema.
It also means (like @Matteo says in his comment) that you can more easily port your project on another type without having to rewrite all your queries.
Using the doctrine QueryBuilder takes this abstraction to the next level.
来源:https://stackoverflow.com/questions/33932415/dql-select-in-doctrine-vs-sql-query