Left join with query builder
In my “new” job – we’re working entirely with Symfony, thus I decided to get my hands dirty and rewrite my eternal “Car Expenses” project in Symfony.
What I found challenging was finding how exactly to make joins in doctrine. So after some digging, trial and error, ended up doing this – code is in a method inside the repository class for an entity.
public function getCarFuels(int $field)
{
return $this->createQueryBuilder('q')
->select('t')
->leftJoin(
'App\Entity\Table',
't',
Join::WITH,
'q.field = t.id'
)
->where('q.field = :field')
->setParameter('field', $field)
->getQuery()
->getArrayResult();
}
This is the equivalent of
SELECT
ft.*
FROM db.first_table q
LEFT JOIN
db.table t on q.field = t.id
WHERE field = n;