Reputation: 31
i do some research to answer it but i don't find.
I'd like to know what is the best practice to do select (with join) between :
use query builder ?
$this->getEntityManager()->createQueryBuilder()
->select('e')
->from('Module\Entity\MyEntity 'e')
->innerJoin('Module\Entity\MyEntity2', 'e2', 'WITH', 'e.e2_id = e2.id')...
->where("...")
or
use SQL statement ?
$db = $this->getEntityManager()->getConnection();
$sql = "SELECT * FROM myEntity e
INNER JOIN myEntity2 AS e2 ON e2.id = e.e2_id....
WHERE ....;"
it is safer, faster,... ?
Upvotes: 3
Views: 2272
Reputation: 1908
Both have advantages and disadvantages, so it depends on what you need.
SQL
DQL
Sometimes I find myself in a situation where I could solve a problem directly in a SQL query, but Doctrine doesn't support some of the constructs I would have to use. So I have to decide if I want to lose the Doctrine benefits and go for the pure SQL solution or use DQL and add some more php code, maybe even more otherwise unnecessary queries. But this depends strongly on the situation and can not be answered in general.
In the end I would use DQL wherever possible because it's easier to write and maintain and only switch to SQL when I need some query to be high performance.
Upvotes: 3