简体   繁体   中英

compare time with symfony2

I searched all over google but was not able to find a solution to my problem. 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

This is what i tried: http://pastebin.com/iA3RF6Mw

Many Thanks :)

[FIXED]

I made a new class whith a bunch of if else statements.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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