Search code examples
symfonydoctrine-ormdoctrinesymfony4doctrine-query

How to add multiple "AND" conditions in a JOIN


I'm trying to add a "AND" condition to the join query but haven't been able to figure it out (not sure if it's even possible via Doctrine/Symfony). I'd appreciate any help with this.

->select('c, p')
->from(Customer::class, 'c')
->leftJoin('c.phones', 'p')

Example:-

SELECT c.*, p.*
FROM customer c
LEFT JOIN phone p ON c.id = p.customer_id AND p.is_main = 1 AND p.category = 0

Solution

  • You can use conditionType of the leftJoin function in queryBuilder check the documentation

        public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null);
    
        Example:
        $qb->leftJoin('c.phones', 'p', 'WITH', 'p.is_main = 1 AND p.category = 0', 'p.id')