我需要将SQL请求转换为DQL请求:
SELECT * FROM object WHERE stardate >= GETDATE() AND enddate <= GETDATE() OR stardate >= GETDATE() AND enddate IS NULL
我尝试过:
$now = new \DateTime();
$qb = $this->createQueryBuilder()
->field('startDate')->lte($now)
->field('endDate')->gte($now);
$qb->addOr(
$qb->expr()
->field('startDate')->lte($now)
->field('endDate')->exists(false)
);
return $qb->getQuery()->execute();
但这不正确。
答案 0 :(得分:2)
解决方案:
$now = new \DateTime('01/01/2018');
$qb = $this->createQueryBuilder();
$qb->addOr(
$qb->expr()
->field('startDate')->lte($now)
->field('endDate')->gte($now)
);
$qb->addOr(
$qb->expr()
->field('startDate')->lte($now)
->field('endDate')->exists(false)
);
return $qb->getQuery()->execute();