Doctrine query builder using inner join with conditions

前端 未结 2 1430
傲寒
傲寒 2020-12-03 04:20

I\'d like to construct the following SQL using Doctrine\'s query builder:

select c.*
from customer c
join phone p
on p.customer_id = c.id
and p.phone = :phon         


        
2条回答
  •  -上瘾入骨i
    2020-12-03 05:17

    I'm going to answer my own question.

    1. innerJoin should use the keyword "WITH" instead of "ON" (Doctrine's documentation [13.2.6. Helper methods] is inaccurate; [13.2.5. The Expr class] is correct)
    2. no need to link foreign keys in join condition as they're already specified in the entity mapping.

    Therefore, the following works for me

    $qb->select('c')
        ->innerJoin('c.phones', 'p', 'WITH', 'p.phone = :phone')
        ->where('c.username = :username');
    

    or

    $qb->select('c')
        ->innerJoin('c.phones', 'p', Join::WITH, $qb->expr()->eq('p.phone', ':phone'))
        ->where('c.username = :username');
    

提交回复
热议问题