如何在条件日期时间内重新控制控制器

时间:2014-05-15 14:05:31

标签: symfony

我需要显示date1和date2之间的需求列表:这是我控制器中的代码:

$finsemaine=new \DateTime('now');  
      $finsemaine = new \DateTime('20:00:00');
      $daysToMove = (int)($finsemaine->format('N')) - 1;


                    if ($daysToMove)
                    {
                         $finsemaine->sub(new \DateInterval('P' . $daysToMove . 'D'));
                    }
                        $finsemaine->format('Y-m-d '); 
                        $finsemaine->modify('+5 day');

//debut semaine
       $debut=new \DateTime('now');  
                         $debut = new \DateTime('08:00:00');

                        $daysToMove = (int)($debut->format('N')) - 1;


                    if ($daysToMove)
                    {
                        $debut->sub(new \DateInterval('P' . $daysToMove . 'D'));
                    }
                        $debut->format('Y-m-d ');

$em = $this->getDoctrine()->getManager();
        $user = $this->get('security.context')->getToken()->getUser();
        $entities = $em->getRepository('PublishDemandsBundle:Appointement')->findBy(array('therapist'=>$user));

我在datebase表中有一个元素:datestart(datetime),我需要显示$ debut和$ finsemaine之间的需求。有人可以帮我修复这个requete。并且想想

1 个答案:

答案 0 :(得分:1)

您需要在AppointementRepository.php中执行自定义请求:

public function findAppointementInTimeFrame($user, \DateTime $debut, \DateTime $finsemaine)
{
    $em = $this->getEntityManager();
    $qb = $em->createQueryBuilder();

    $qb->select('a')
       ->from('PublishDemandsBundle:Appointement','a')
       ->where($qb->expr()->andX(
             $qb->expr()->gt('a.datestart', ':debut'),
             $qb->expr()->lt('a.dateStart', ':finsemaine'),
             $qb->expr()->eq('a.therapist', ':therapist'),
             ))
       ->setParameter('debut', $debut)
       ->setParameter('finsemaine', $finsemaine);
       ->setParameter('therapist', $user);

    return $qb->getQuery()
              ->getResult();
}