简体   繁体   English

使用带有Symfony2的Doctrine DQL检查日期是否在-45min + 45min范围内

[英]Check if date is in range -45min +45min using Doctrine DQL with Symfony2

I want to query my database using DQL and find out if there is any record in this table of mine wich collides with one that I'm about to insert, the thing is that I cannot insert records if the time column collides with them. 我想使用DQL查询我的数据库,并找出该表中是否有任何记录与我要插入的记录冲突,问题是如果时间列与它们冲突,则无法插入记录。

Each entity represents an Appointment so they have a duration of at least, 45min, and thats what I want my query to check. 每个实体代表一个约会,因此它们的持续时间至少为45分钟,这就是我要查询的内容。 This is what I've come up with: 这是我想出的:

  1. If every appointment has a duration of 45min, then I would have to check if there is already a record in the database in the range of (Appointment.time - 45min, Appointment.time) 如果每个约会的持续时间为45分钟,那么我将必须检查数据库中是否已经有(Appointment.time-45min,Appointment.time)范围内的记录。
  2. Can't just make unique the time column and be happy because of the duration matter, we're better than that. 由于持续时间的问题,不能仅仅使时间栏变得唯一并感到高兴,我们比这更好。

The code I have so far and that doesn't work is this :D 到目前为止,我没有用的代码是:D

$query = $em->createQuery('SELECT a FROM AcmeStuff\ApiBundle\Entity\Appointment a
                          WHERE a.time BETWEEN ?1 AND ?2
                          ORDER BY a.date DESC');
$query->setParameter(1, $appointment->getDate()->sub(new \DateInterval('PT45M')));
$query->setParameter(1, $appointment->getDate());

Can someone please point me in the right direction, feels like I'm far from it. 有人能指出我正确的方向吗,感觉我离它还很远。

EDIT: 编辑:

I'm not getting the records that I should be getting, what is the correct way to query for a range of dates using DQL? 我没有得到应该得到的记录,使用DQL查询日期范围的正确方法是什么?

Thanks in advance! 提前致谢!

你需要这样写

createQuery('SELECT a FROM AcmeStuff\ApiBundle\Entity\Appointment a ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM