错误:执行Symfony时发生异常

时间:2017-08-09 19:00:10

标签: symfony doctrine-orm

尝试执行自定义存储库时出错:

  

执行'SELECT COUNT(p0_.id)AS sclr0从PayrollWeek p1_ WITH(NOLOCK),PayrollWeek p0_ WITH(NOLOCK)WHERE p0_.startDate =? AND p0_.endDate =?' with params [“startDate”,“endDate”]:

     

SQLSTATE [22007]:[Microsoft] [SQL Server的ODBC驱动程序11] [SQL Server]从字符串转换日期和/或时间时转换失败。我有什么

public function createAction(Request $request)
{


    $entity = new Payrollperiod();

    $form = $this->createCreateForm($entity);

    $form->handleRequest($request);


        if ($form->isSubmitted() && $form->isValid()) { 

        $em = $this->getDoctrine()->getManager();

        $payrollperiod = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->findOneBy(['startdate'=>$form->get('startDate')->getData(), 'enddate'=>$form->get('endDate')->getData()]);

        $payrollweek = $em->getRepository('comtwclagripayrollBundle:Payrollweek')->findBystartdateAndenddate($form->get('startDate')->getData(), $form->get('endDate')->getData());

        $counter = $em->getRepository('comtwclagripayrollBundle:Payrollweek')->countBystartdateAndenddate('startDate', 'endDate');

        if ($counter){
        //If entity exists
        if ($payrollperiod){
                $this->addFlash('error', 'Error: A payroll period is already present with the same start and end date.');
                return $this->redirect($this->generateUrl('payrollperiod'));
            }
            //If PayrollWeek matches the Payrollperiod entered
           elseif ($payrollweek){
            foreach ($payrollweek as $pWeek) {
                $pWeek->setPayrollperiodid($entity); 
                $em->persist($pWeek);
            }

          $em->persist($entity);
          $em->flush();

            $this->addFlash('success', 'Payroll Period was added.');
            $this->addFlash('success', 'Payroll week was updated.');
            return $this->redirect($this->generateUrl('payrollperiod'));
           //return $this->redirect($this->generateUrl('payrollperiod_show', array('payrollperiodid' => $entity->getpayrollperiodid())));
             }
             else{
               $this->addFlash('error', 'Payroll Period does not match a payroll week.');
            return $this->redirect($this->generateUrl('payrollperiod'));  
             }
       }

       //return $this->render('comtwclagripayrollBundle::new.html.twig',array('form' => $form->createView()));
       return array(
           'entity' => $entity,
           'form'   => $form->createView(),
       );   

        }

}

     public function findBystartdateAndenddate($startdate, $enddate)
    {

       return $this
        ->createQueryBuilder('pw')
         ->select('pw')
         ->where ('pw.startdate = :startdate and pw.enddate = :enddate')
        ->setParameter('startdate', $startdate)
        ->setParameter('enddate', $enddate)
        ->getQuery()
        ->getResult();
    }

   public function countBystartdateAndenddate($startdate, $enddate)
    {
        return $this
        ->createQueryBuilder('payrollweek')
         ->select('COUNT(pw)')
         ->from ('comtwclagripayrollBundle:Payrollweek','pw')
         ->where ('pw.startdate = :startdate and pw.enddate = :enddate')
        ->setParameter('startdate', $startdate)
        ->setParameter('enddate', $enddate)
        ->getQuery()
        ->getOneorNullResult();
    }

我正在尝试计算工资核算周以匹配输入的工资核算期。工资核算期为两周,而工资核算周为一周。例如,用户在2017年8月6日至8月19日进入,那么它将在8月6日至12日的工资表中输入两个记录。 8月13日至19日。一个计数会为此工作吗?

1 个答案:

答案 0 :(得分:1)

不要使用$form->get('startDate')$form->get('endDate')尝试使用$entity->getStartDate()$entity->getEndDate(),因为您要传递字符串而不是Datetime对象