简体   繁体   English

与symfony2比较时间

[英]compare time with symfony2

I searched all over google but was not able to find a solution to my problem. 我在整个Google上进行了搜索,但找不到解决我问题的方法。 So i have this system where people are able to set tasks on a specific time. 所以我有这个系统,人们可以在特定的时间设置任务。

When the time has already a task i should throw an error letting the person know that the time is already reserved and that it won't be written to the database. 当时间已经完成时,我应该抛出一个错误,让该人知道该时间已被保留,并且不会被写入数据库。 But for some reason im not able to accomplish this. 但是由于某种原因,我无法完成此任务。

I think the problem has to do with the if statement 我认为问题与if语句有关

This is what i tried: http://pastebin.com/iA3RF6Mw 这就是我尝试过的: http : //pastebin.com/iA3RF6Mw

Many Thanks :) 非常感谢 :)

[FIXED] [固定]

I made a new class whith a bunch of if else statements. 我做了一堂新课,上面写了一堆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;
    }

}

It's hard to catch whole logic but it is certainly wrong what you do: 很难理解整个逻辑,但是您做的事情肯定是错误的:

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

You make lookup to "Name" by date fieled 您按申请日期对“名称”进行查询

And regarding how to check entity in date range, you should make your own repository method like 关于如何检查日期范围内的实体,您应该制作自己的存储库方法,例如

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();
}

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

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