问题描述
我指定了一个报告日期。(报告实体已提交 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)')