我正在使用 symfony 2.7 表单组件来更新名为TapsAlert
的实体。
问题是提交表单后实体没有更新。
我不知道出了什么问题。感谢您的建议,这是部分代码:
public function editarRegistroAction(Request $request, $id){
$em = $this->getDoctrine()->getManager();
$altd = $em->getRepository('ModeloBundle:TapsAlert')->find($id);
$altd->setAsunto($altd->getAsunto());
$altd->setGls($altd->getGls());
$altd->setDestinatario($altd->getDestinatario());
$altd->setPrts($altd->getPrts());
$altd->setTags($altd->getTags());
$altd->setValor($altd->getValor());
$form = $this->createFormBuilder($altd)
->add('Asunto', 'text')
->add('gls', 'text')
->add('destinatario', 'text')
//->add('dias', 'number')
->add('prts', 'text')
->add('Tags', 'text')
->add('valor', 'text')
->add('save', 'submit', array('label' => 'Guardar Cambios'))
->getForm();
//$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//$edAlert = $form->getData();
$em = $this->getDoctrine()->getManager();
//$em->persist($altd);
$em->flush();
return $this->redirect('http://192.168.1.128/');
}
return $this->render('MotoBundle:Default:edit.html.twig', array('form' => $form->createView()));
}
答案 0 :(得分:2)
首先:这部分代码没有用。应该将其删除:
$altd->setAsunto($altd->getAsunto());
$altd->setGls($altd->getGls());
$altd->setDestinatario($altd->getDestinatario());
$altd->setPrts($altd->getPrts());
$altd->setTags($altd->getTags());
$altd->setValor($altd->getValor());
第二:您未处理您的请求:
public function editarRegistroAction(Request $request, $id){
$em = $this->getDoctrine()->getManager();
$altd = $em->getRepository('ModeloBundle:TapsAlert')->find($id);
$form = $this->createFormBuilder($altd)
->add('Asunto', 'text')
->add('gls', 'text')
->add('destinatario', 'text')
//->add('dias', 'number')
->add('prts', 'text')
->add('Tags', 'text')
->add('valor', 'text')
->add('save', 'submit', array('label' => 'Guardar Cambios'))
->getForm();
$form->handleRequest($request); //uncomment this line
if ($form->isSubmitted() && $form->isValid()) {
$em->flush();
return $this->redirect('http://192.168.1.128/'); // This url should an be an external url
}
return $this->render('MotoBundle:Default:edit.html.twig', array('form' => $form->createView()));
}
第三步:我相信您可以使用paramConvertor来改进代码:
public function editarRegistroAction(Request $request, TapsAlert $tapsAlert){
$form = $this->createFormBuilder($tapsAlert)
->add('Asunto', 'text')
->add('gls', 'text')
->add('destinatario', 'text')
//->add('dias', 'number')
->add('prts', 'text')
->add('Tags', 'text')
->add('valor', 'text')
->add('save', 'submit', array('label' => 'Guardar Cambios'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirect('http://192.168.1.128/'); // This url should an be an external url
}
return $this->render('MotoBundle:Default:edit.html.twig', array('form' => $form->createView()));
}
这是一种更好的做法,也可以在单独的类中构建表单,有关更多详细信息,请查看官方文档https://symfony.com/doc/current/forms.html#creating-form-classes