如何在 Doctrine 中使用 conditionType 'ON'?
How to use conditionType 'ON' with Doctrine?
我对 Doctrine 关于条件 ON
有疑问。我读过 here:
Now in doctrine 2 I have never seen a situation where ON can be used.
In fact using ON always ends up in an exception saying you should use
WITH.
但是我需要重新定义连接条件。这是我的查询:
$qb = $this->createQueryBuilder('fu');
$qb
->addSelect('cra')
->innerJoin('fu.chatRoomAdmins', 'cra')
->where('cra.operator = :operatorId')
->setParameter('operatorId', $operatorId);
$foreignUsers = $qb->getQuery()->getResult();
这里是(重要部分)生成的 sql 查询(我将 table 的名称更改为更具可读性):
SELECT *
FROM fu
INNER JOIN cra
ON fu.id = cra.operator_id
WHERE ( cra.operator_id = 'an_id');
但我需要将 ON
条件更改为:ON fu.id = cra.operator_id OR cra.operator_id IS NULL
如果我像这样使用 WITH
条件:
$qb
//..
->innerJoin('fu.chatRoomAdmins', 'cra', 'WITH', 'cra.operator IS NULL')
//..
$foreignUsers = $qb->getQuery()->getResult();
它改变了我的查询:
SELECT *
FROM fu
INNER JOIN cra
ON fu.id = cra.operator_id
AND ( cra.operator_id IS NULL )
WHERE ( cra.operator_id = 'an_id' );
但我想要这个 ON
条件:ON fu.id = cra.operator_id OR ( cra.operator_id IS NULL )
这就是为什么我试图在我的 queryBuilder 中用 ON
替换 WITH
的原因:
$qb
//..
->innerJoin('fu.chatRoomAdmins', 'cra', 'ON', 'cra.operator IS NULL')
//..
$foreignUsers = $qb->getQuery()->getResult();
但现在我得到一个错误:Expected end of string, got 'ON'
(我使用的是 symfony 4)
你知道有什么方法可以达到这个 ON
条件:ON fu.id = cra.operator_id OR ( cra.operator_id IS NULL )
吗?
我找到了一种方法 leftJoin
:
$qb = $this->createQueryBuilder('fu');
$qb
->addSelect('cra')
->leftJoin('fu.chatRoomAdmins', 'cra')
->where('cra.operator = :operatorId OR cra.operator IS NULL');
$foreignUsers = $qb->getQuery()->getResult();
给这个 SQL:
SELECT *
FROM fu
LEFT JOIN cra
ON fu.id = cra.operator_id
WHERE ( cra.operator_id = 'an_id'
OR cra.operator_id IS NULL );
它是这样工作的
$query->innerJoin('App\Domains\Order\Domain\Models\Order\B2bOrderExt', 'b2b', \Doctrine\ORM\Query\Expr\Join::WITH, 'o.id=b2b.orderId');
我对 Doctrine 关于条件 ON
有疑问。我读过 here:
Now in doctrine 2 I have never seen a situation where ON can be used. In fact using ON always ends up in an exception saying you should use WITH.
但是我需要重新定义连接条件。这是我的查询:
$qb = $this->createQueryBuilder('fu');
$qb
->addSelect('cra')
->innerJoin('fu.chatRoomAdmins', 'cra')
->where('cra.operator = :operatorId')
->setParameter('operatorId', $operatorId);
$foreignUsers = $qb->getQuery()->getResult();
这里是(重要部分)生成的 sql 查询(我将 table 的名称更改为更具可读性):
SELECT *
FROM fu
INNER JOIN cra
ON fu.id = cra.operator_id
WHERE ( cra.operator_id = 'an_id');
但我需要将 ON
条件更改为:ON fu.id = cra.operator_id OR cra.operator_id IS NULL
如果我像这样使用 WITH
条件:
$qb
//..
->innerJoin('fu.chatRoomAdmins', 'cra', 'WITH', 'cra.operator IS NULL')
//..
$foreignUsers = $qb->getQuery()->getResult();
它改变了我的查询:
SELECT *
FROM fu
INNER JOIN cra
ON fu.id = cra.operator_id
AND ( cra.operator_id IS NULL )
WHERE ( cra.operator_id = 'an_id' );
但我想要这个 ON
条件:ON fu.id = cra.operator_id OR ( cra.operator_id IS NULL )
这就是为什么我试图在我的 queryBuilder 中用 ON
替换 WITH
的原因:
$qb
//..
->innerJoin('fu.chatRoomAdmins', 'cra', 'ON', 'cra.operator IS NULL')
//..
$foreignUsers = $qb->getQuery()->getResult();
但现在我得到一个错误:Expected end of string, got 'ON'
(我使用的是 symfony 4)
你知道有什么方法可以达到这个 ON
条件:ON fu.id = cra.operator_id OR ( cra.operator_id IS NULL )
吗?
我找到了一种方法 leftJoin
:
$qb = $this->createQueryBuilder('fu');
$qb
->addSelect('cra')
->leftJoin('fu.chatRoomAdmins', 'cra')
->where('cra.operator = :operatorId OR cra.operator IS NULL');
$foreignUsers = $qb->getQuery()->getResult();
给这个 SQL:
SELECT *
FROM fu
LEFT JOIN cra
ON fu.id = cra.operator_id
WHERE ( cra.operator_id = 'an_id'
OR cra.operator_id IS NULL );
它是这样工作的
$query->innerJoin('App\Domains\Order\Domain\Models\Order\B2bOrderExt', 'b2b', \Doctrine\ORM\Query\Expr\Join::WITH, 'o.id=b2b.orderId');