比较时间与symfony2

时间:2015-05-13 11:20:13

标签: php symfony

我搜索了所有谷歌,但无法找到我的问题的解决方案。 所以我有这个系统,人们可以在特定的时间设置任务。

当时间已经完成任务时,我应该抛出错误让这个人知道 时间已经保留,并且不会写入数据库。 但由于某种原因,我无法做到这一点。

我认为问题与if语句

有关

这就是我尝试过的:http://pastebin.com/iA3RF6Mw

非常感谢:)

2 个答案:

答案 0 :(得分:1)

[固定]

我用一堆if else语句创建了一个新类。

public function checkBooking($booking,$em){

    $reservations = $em->getRepository("AppBundle:Applicant")->findBy(array("date" => $booking->getDate(),"room" => $booking->getRoom()));
    $errors = 0;
    $bookingTimeStart = $booking->getTimeStart()->format('H:i');
    $bookingTimeEnd = $booking->getTimeEnd()->format('H:i');
    foreach($reservations as $reservation){
        $timeStart = $reservation->getTimeStart()->format('H:i');
        $timeEnd = $reservation->getTimeEnd()->format('H:i');

        if($bookingTimeStart >= $timeStart && $bookingTimeStart <= $timeEnd){
            $errors++;
        }
        if($bookingTimeEnd >= $timeStart && $bookingTimeEnd <= $timeEnd){
            $errors++;
        }
        if($bookingTimeStart <= $timeStart && $bookingTimeEnd >= $timeEnd){
            $errors++;
        }

    }
    $error = array();
    if($errors > 0){
        $error[] = "THis room is already occupied.";
    }
    if($bookingTimeEnd < $bookingTimeStart){
        $error[] = "The end time cant be bigger than the begin time.";
    }

    if(count($error) == 0){
        $em->persist($booking);
        $em->flush();
        return array();
    }else{
        return $error;
    }

}

答案 1 :(得分:0)

很难理解整个逻辑,但你做的事情肯定是错误的:

 $time_start = $em->getRepository("AppBundle:book")->findByName($form->get("timeStart") -> getViewData());

您按日期查询“名称”

关于如何在日期范围内检查实体,您应该创建自己的存储库方法,如

public function findInRange($timeStart, $timeEnd)
{
   $qb->select('b')
   ->from('Book','b')
   ->add('where', $qb->expr()->between(
            'b.date',
            ':from',
            ':to'
        )
    )
   ->setParameters(array('from' => $startDate, 'to' => $endDate));
  $query = $qb->getQuery();
  return $query->getResult();
}