gpt4 book ai didi

php - Doctrine - DQL - 多个 OR 嵌套在一个封装 AND 中

转载 作者:行者123 更新时间:2023-12-02 03:44:40 25 4
gpt4 key购买 nike

我找不到 DQL 的示例,半伪代码如下所示:

Bring back invoices
- Where company id = 5
AND
(
->where('DATE(i.startPeriod) BETWEEN :startDate AND :endDate')
->orWhere('DATE(i.endPeriod) BETWEEN :startDate AND :endDate')
->orWhere(':startDate BETWEEN DATE(i.startPeriod) and DATE(i.endPeriod)')
->orWhere(':endDate BETWEEN DATE(i.startPeriod) and DATE(i.endPeriod)')
)

因此,您有四个 OR 嵌套在一个封装 AND 中。

有人知道如何使用 Doctrine DQL 做到这一点吗?将一堆 OR 嵌套在一个巨大的 AND 中?

最佳答案

您需要将 Expr() 类与查询生成器一起使用。

// $qb instanceof QueryBuilder

$qb->select('i')
->from('invoices', 'i')
->where('c.id = :cid')
->andWhere($qb->expr()->orX(
$qb->expr()->between('i.startPeriod',':startdate',':enddate'),
$qb->expr()->between('i.endPeriod',':startdate',':enddate'),
...

您可以在 the documentation 中阅读有关 Expr() 类的更多信息。 .

编辑:

刚刚意识到您最初提出的问题专门涉及 DQL。您可以在 DQL 中使用括号对事物进行分组,就像这样。

$query = $em->createQuery(
'SELECT i FROM Invoices i
WHERE c.id = :id
AND (
(i.startPeriod BETWEEN :startDate AND :endDate)
OR
(i.endPeriod BETWEEN :startDate AND :endDate)
OR
(:startDate BETWEEN i.startPeriod AND i.endPeriod)
OR
(:endDate BETWEEN i.startPeriod AND i.endPeriod)
) JOIN i.company c');

关于php - Doctrine - DQL - 多个 OR 嵌套在一个封装 AND 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47191656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com