Symfony-如果结束日期为空,则为主义过滤日期

问题描述

我有一个棘手的问题,我想了好几天才能解决此问题。

我有报告订阅

我指定了一个报告日期。(报告实体已提交 reportDate

想法是在该特定报告日返回一个结果。

然后在查询构建器中,我触发具有 startDate endDate 归档的Subscriptions表。 想法是在存在于reportDate日期范围内的“订阅”中查找表格行。

因此,我正在过滤“订阅”,以查找在特定日期有效的订阅

如果在我的表单中输入 2020-11-01至2020-11-10 ,它会过滤自定义的foreach数组并返回例外结果。

问题:endDate是可选的,可以具有NULL值。 我要完成的是,如果 endDate IS NULL 可以过滤从startDate到将来的所有结果。现在,当我遍历所有订阅时,结果始终为0。

处理它的方法

public function getSubscription($reportId,DateTime $reportDate)
{
    $from = new DateTime($reportDate->format("Y-m-d")." 00:00:00");
    $to   = new DateTime($reportDate->format("Y-m-d")." 23:59:59");

    return $this->createqueryBuilder("e")
        ->where('e.reportId =:reportId')
        ->andWhere('e.startDate <= :from')
        ->andWhere('e.endDate >= :to')
        ->setParameter('reportId',$reportId)
        ->setParameter('from',$from)
        ->setParameter('to',$to)
        ->getQuery()
        ->getoneOrNullResult();
}

还尝试添加

->andWhere('e.endDate >= :to AND (e.endDate IS NULL OR e.endDate >= :to)')

结果是相同的。

解决方法

您非常亲密。您的问题是这样的:e.endDate >= anything为空时e.endDate为假。

尝试一下,简化您在问题中提到的内容。

->andWhere('(e.endDate IS NULL OR e.endDate >= :to)')