收到错误:
变量“形式”不存在于 comtwclagripayrollBundle:Payrollperiod:第9行的new.html.twig
我不确定更新是否已正确完成,但基本上我希望在匹配后更新带有工资核算期的工资核算周。存储库检查它。
存储库:
public function findByPayrollPeriod($startDate, $endDate)
{
return $this->getEntityManager()
->createQuery(
'SELECT p FROM comtwclagripayrollBundle:PayrollWeek
WHERE startDate = :startDate or endDate = :endDate'
)
->setParameter('startDate', $startDate)
->setParameter('endDate', $endDate)
->getResult();
}
控制器:
public function createAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$entity = new Payrollperiod();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
$payrollperiod = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->findOneBy(['startdate'=>$form->get('startDate')->getData(), 'enddate'=>$form->get('endDate')->getData()]);
//If entity exists
if ($payrollperiod){
$this->addFlash('error', 'Payroll Period exist.');
return $this->redirect($this->generateUrl('payrollperiod'));
}
if ($form->isSubmitted() && $form->isValid()) {
$payrollweek = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->findByPayrollPeriod(['startdate'=>$form->get('startDate')->getData(), 'enddate'=>$form->get('endDate')->getData()]);
if ($payrollweek){
$entity->setPayrollweek($payrollweek);
$em->persist($entity);
// $em->persist($payrollweek);
$em->flush();
$this->addFlash('error', 'Payroll Period was added.');
return $this->redirect($this->generateUrl('payrollperiod'));
//return $this->redirect($this->generateUrl('payrollperiod_show', array('payrollperiodid' => $entity->getpayrollperiodid())));
}
return $this->render('comtwclagripayrollBundle::new.html.twig',array('form' => $form->createView()));
}
}
private function createCreateForm(Payrollperiod $entity)
{
$form = $this->createForm(new PayrollperiodType(), $entity, array(
'action' => $this->generateUrl('payrollperiod_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
嫩枝:
{% extends '::base.html.twig' %}
{% block body -%}
<div id="pg_section_sml_card">
<h1 class="pg_card_hdr">Payroll Period</h1>
<div class="pg_card_text">
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_end(form) }}
<ul class="record_actions">
<li>
<a href="{{ path('payrollperiod') }}">
Back to the list
</a>
</li>
</ul>
</div>
</div>
{% endblock %}
答案 0 :(得分:0)
除非你有一个有效的方法叫
createCreateForm()
,那么您没有将有效的Symfony表单组件返回到$form
,您应该传入表单类型和实体类型,如
$form = $this-createForm(FormType::class, $entity);
哪个表单类型是您要创建的表单,您能确认问题是什么吗?我看不到您在createAction()
答案 1 :(得分:0)
这就是我为实现这一目标所做的工作。
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', 'Payroll Period exist.');
return $this->redirect($this->generateUrl('payrollperiod'));
}
//If PayrollWeek matches the Payrollperiod entered
elseif ($payrollweek){
//This adds the payroll period id to the payroll week table
foreach($payrollweek as $pWeek) {
$pWeek->setPayrollperiodid($entity);
//$em->persist($pWeek);
}
$em->persist($entity);
$em->flush();
$this->addFlash('error', 'Payroll Period was added.');
$this->addFlash('error', '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(),
);
}