php - Error: Call to a member function setPayrollperiodid() on null -
i'm trying count number of payroll week matches payroll period entered. returns correct number won't add database instead says there's no match in database. when make changes error:
error: call member function setpayrollperiodid() on null.
i have outlined have far. can tell me went wrong.
i have made updates question no longer error record isn't adding instead it's saying doesn't match did go wrong in 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: payroll period present same start , end date.'); return $this->redirect($this->generateurl('payrollperiod')); } //if payrollweek matches payrollperiod entered elseif ( $counter==2){ $payrollweek->setpayrollperiodid($entity); } $em->persist($entity); $em->flush(); $this->addflash('success', 'payroll period added.'); $this->addflash('success', 'payroll week updated.'); return $this->redirect($this->generateurl('payrollperiod')); //return $this->redirect($this->generateurl('payrollperiod_show', array('payrollperiodid' => $entity->getpayrollperiodid()))); } else{ $this->addflash('error', 'payroll period not match 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: payroll period present same start , end date.'); return $this->redirect($this->generateurl('payrollperiod')); } //if payrollweek matches payrollperiod entered elseif ($counter==2){ foreach ($counter $pweek) { $pweek->setpayrollperiodid($entity); } $em->persist($entity); $em->flush(); $this->addflash('success', 'payroll period added.'); $this->addflash('success', 'payroll week updated.'); return $this->redirect($this->generateurl('payrollperiod')); //return $this->redirect($this->generateurl('payrollperiod_show', array('payrollperiodid' => $entity->getpayrollperiodid()))); } else{ $this->addflash('error', 'payroll period not match 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: payroll period present same start , end date.'); return $this->redirect($this->generateurl('payrollperiod')); } //if payrollweek matches payrollperiod entered elseif ($payrollweek){ $count = count($payrollweek); foreach ($payrollweek $pweek) { $pweek->setpayrollperiodid($entity); } $em->persist($entity); $em->flush(); $this->addflash('success', 'payroll period added.'); $this->addflash('success', 'payroll week updated.'); return $this->redirect($this->generateurl('payrollperiod')); //return $this->redirect($this->generateurl('payrollperiod_show', array('payrollperiodid' => $entity->getpayrollperiodid()))); } else{ $this->addflash('error', 'payroll period not match 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(), ); }
you're not checking $payrollweek
exists before trying use it. in case, didn't find value when ran function:
$payrollweeks = $em->getrepository('comtwclagripayrollbundle:payrollweek') ->findbystartdateandenddate($entity->getstartdate() , $entity->getenddate());
since didn't find anything, $payrollweek
null
instead of instance of payrollweek entity. add check this:
if (!$payrollweeks) { $this->addflash( 'error', 'error: no payroll week found given start , end date' ); return $this->redirect($this->generateurl('payrollperiod')); }
edit based on discussions:
you should able rid of countbystartdateandenddate()
function , use php's count() function results want, loop on payroll weeks:
$count = count($payrollweeks); foreach ($payrollweeks $payrollweek) { $payrollweek->setpayrollperiodid($entity); }
Comments
Post a Comment