如何在 JOIN 中添加多个 "AND" 条件

How to add multiple "AND" conditions in a JOIN

我正在尝试向连接查询添加一个 "AND" 条件,但一直无法弄清楚(不确定是否可以通过 Doctrine/Symfony 实现)。我将不胜感激。

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

示例:-

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

您可以在 queryBuilder 中使用 leftJoin 函数的 conditionType 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')

像这样使用 WITH 选项:

$qb->leftJoin('c.phones', 'p', 'WITH', 'p.is_main = 1 AND p.category = 0', 'p.id')

Docs