doujiangqu2823 2017-08-14 15:59
浏览 50
已采纳

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

I'm trying to count the number of payroll week that matches the payroll period entered. It returns the correct number but won't add to the database but instead says there's no match in the database. When I make changes I get this error:

Error: Call to a member function setPayrollperiodid() on null.

I have outlined what I have thus far. Can someone tell me where I went wrong.

I have made some updates to the question I no longer get that error but record isn't adding instead it's saying that it doesn't match where did I go wrong in the controller.

Repository

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

Controller

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

UPDATES

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

        }

Further Updates

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条回答 默认 最新

  • duanmen8491 2017-08-14 16:06
    关注

    You're not checking that $payrollweek exists before trying to use it. In your case, it didn't find a value when you ran this function:

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

    Since it didn't find anything, $payrollweek is NULL instead of an instance of your Payrollweek entity. You could add another check like this:

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

    EDIT based on discussions:

    You should be able to get rid of your countBystartdateAndenddate() function and just use PHP's count() function to get the results you want, then loop over your payroll weeks:

    $count = count($payrollweeks);
    
    foreach ($payrollweeks as $payrollweek) {
        $payrollweek->setPayrollperiodid($entity); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 quartus II 9.0闪退问题怎么解决啊,急
  • ¥20 求自动化运维语料数据集
  • ¥30 广告检测流量作弊案例 IDEA运行代码报错 连接不上metastore 检测了环境配置没有问题 请求远程解决加VX问细节问题 不加的不回复
  • ¥15 matlab图像融合代码被嫌弃太简单,求改进。第一步改成直接读取三张图片,不读取文件夹
  • ¥20 微处理器原理与应用(私有偿)
  • ¥50 8051单片机关于ADC0809的应用
  • ¥15 有没有能拿来练练手写完发给我
  • ¥15 禁止修改windows系统时间
  • ¥50 kinect连接win11笔电导致音视频设备消失
  • ¥15 python线性查找题