错误:在null上调用成员函数setPayrollperiodid()

时间:2017-08-14 15:59:34

标签: php symfony doctrine-orm

我正在尝试计算与输入的工资核算期间相匹配的工资核算周数。它返回正确的数字,但不会添加到数据库,而是说数据库中没有匹配项。当我进行更改时,我收到此错误:

  

错误:在null上调用成员函数setPayrollperiodid()。

我已经概述了我到目前为止的情况。有人能告诉我哪里出错了。

我已经对问题做了一些更新,我不再收到错误,但记录没有添加,而是说它与控制器中出错的位置不匹配。

存储库

class PayrollweekRepository extends EntityRepository
{
    public function findBystartdateAndenddate($startdate, $enddate)
    {
        $repository = $this->getEntityManager()
            ->getRepository('comtwclagripayrollBundle:Payrollweek');

        $qb = $repository->createQueryBuilder('pw');
        $qb->select('pw');
        $qb->where('pw.startdate=:startdate');
        $qb->Andwhere('pw.enddate=:enddate');
        $qb->setParameter('startdate', $startdate);
        $qb->setParameter('enddate', $enddate);
        $qb->getquery()->getResult();
    }

    public function countBystartdateAndenddate($startdate, $enddate)
    {
        $repository = $this->getEntityManager()
            ->getRepository('comtwclagripayrollBundle:Payrollweek');

        $qb = $repository->createQueryBuilder('pw');
        $qb->select('count(pw.id)');
        $qb->where('pw.startdate=:startdate or pw.enddate=:enddate');
        $qb->setParameter('startdate', $startdate);
        $qb->setParameter('enddate', $enddate);

        $count = $qb->getQuery()->getSingleScalarResult();

        var_dump($count);
        return $count;
    }
}

控制器

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($entity->getStartDate() , $entity->getEndDate());

        $counter = $em->getRepository('comtwclagripayrollBundle:Payrollweek')
            ->countBystartdateAndenddate($entity->getStartDate() , $entity->getEndDate());

        //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 ( $counter==2){
           $payrollweek->setPayrollperiodid($entity); 
        }

        $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 countBystartdateAndenddate($startdate, $enddate)
{
    $repository = $this->getEntityManager()
        ->getRepository('comtwclagripayrollBundle:Payrollweek');

    $qb = $repository->createQueryBuilder('pw');
    $qb->select('count(pw.id)');
    $qb->where('pw.startdate=:startdate or pw.enddate=:enddate');
    $qb->setParameter('startdate', $startdate);
    $qb->setParameter('enddate', $enddate);

    $count = $qb->getQuery()->getResult();

    var_dump($count);
    return $count;
}

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($form->get('startDate')->getData(), $form->get('endDate')->getData());


        //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 ($counter==2){
            foreach ($counter as $pWeek) {

                $pWeek->setPayrollperiodid($entity); 

            }

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



        //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){  
            $count = count($payrollweek);
            foreach ($payrollweek as $pWeek) {
            $pWeek->setPayrollperiodid($entity); 
            } 

            $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(),
       );   

        }

1 个答案:

答案 0 :(得分:0)

在尝试使用之前,您没有检查$payrollweek是否存在。在您的情况下,它在您运行此函数时找不到值:

$payrollweeks = $em->getRepository('comtwclagripayrollBundle:Payrollweek')
    ->findBystartdateAndenddate($entity->getStartDate() , $entity->getEndDate());

由于没有找到任何内容,$payrollweekNULL而不是 Payrollweek 实体的实例。你可以添加另一个这样的支票:

if (!$payrollweeks) {
    $this->addFlash(
        'error',
        'Error: No payroll week found with the given start and end date'
    );

    return $this->redirect($this->generateUrl('payrollperiod'));
}

编辑基于讨论:

你应该能够摆脱你的countBystartdateAndenddate()功能,只需使用PHP的count()功能来获得你想要的结果,然后循环你的工资周:

$count = count($payrollweeks);

foreach ($payrollweeks as $payrollweek) {
    $payrollweek->setPayrollperiodid($entity); 
}