我正在使用Symfony2 Framework,我想用表单中的数据更新实体。
我使用相同的控制器用一些数据填充表单,同时我用它来在数据库中进行更新查询。如果我使用$ em-> persist($ foo)它确实保存了我想要的内容,但我不想像它的新数据一样保存它,我想要更新。
阅读symfony2书,它说$ em-> flush()就是我们所需要的,所以我们可以更新。
我觉得我很亲密,但当然我错过了什么。
以下是代码:
public function actualizarCurriculoAction($id){
$curriculo = new Curriculittle();
$form = $this->createForm(new CurriculittleType(), $curriculo);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
$lselect=$this->get('request')->request->get('lselect');
$edad=$this->get('request')->request->get('edad');
$estado=$this->get('request')->request->get('estadoselect');
if ($form->isValid()) {
$curriculo->setUsalentes($lselect);
$curriculo->setEdad($edad);
$curriculo->setEstado($estado);
$em = $this->getDoctrine()->getEntityManager();
/*em->persist($curriculo);*/
$em->flush(); /*the above line is in comment because I just want to update*/
/*At this point the entity should be updated, but it's not*/
/*Llamando a la plantilla de listados*/
$curriculos = $em->getRepository('SofLaSoflaBundle:Curriculittle')->findAll();
/*Enviando los datos a la plantilla y Renderizandola*/
return $this->render('SofLaSoflaBundle:Default:listado.html.twig', array('curriculos' => $curriculos));
}
}
$em = $this->getDoctrine()->getEntityManager();
$trabajador=$em->getRepository('SofLaSoflaBundle:Curriculittle')->find($id);
return $this->render('SofLaSoflaBundle:Default:curriculo.html.twig', array('form' => $form->createView(), 'curriculo' => $trabajador));
}
所以,请帮忙。 :)
答案 0 :(得分:5)
在您的示例中,您要创建一个新实体:
$curriculo = new Curriculittle();
由于这不是预先存在的实体(例如,您通过数据库查询检索到的实体),因此调用$em->persist($curriculo)
后跟$em->flush()
会将此实体保存为新项目。 更新没有任何内容,因为该实体是新的。
用于持久化和刷新的相同代码可用于更新现有实体;您只需要先检索/获取现有实体,而不是创建新实体。目前看起来你正在这个方法结束时这样做:
$trabajador=$em->getRepository('SofLaSoflaBundle:Curriculittle')->find($id);
然而,您应该在绑定表单之前执行此操作,例如:
public function actualizarCurriculoAction($id) {
$em = $this->getDoctrine()->getEntityManager();
$curriculo = $em->getRepository('SofLaSoflaBundle:Curriculittle')->find($id);
if (!$curriculo) {
$curriculo = new Curriculittle();
}
$form = $this->createForm(new CurriculittleType(), $curriculo);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
$lselect=$this->get('request')->request->get('lselect');
$edad=$this->get('request')->request->get('edad');
$estado=$this->get('request')->request->get('estadoselect');
if ($form->isValid()) {
$curriculo->setUsalentes($lselect);
$curriculo->setEdad($edad);
$curriculo->setEstado($estado);
$em->persist($curriculo);
$em->flush();
/*Llamando a la plantilla de listados*/
$curriculos = $em->getRepository('SofLaSoflaBundle:Curriculittle')->findAll();
/*Enviando los datos a la plantilla y Renderizandola*/
return $this->render('SofLaSoflaBundle:Default:listado.html.twig', array('curriculos' => $curriculos));
}
}
return $this->render('SofLaSoflaBundle:Default:curriculo.html.twig', array('form' => $form->createView(), 'curriculo' => $curriculo));
}
此示例(adjust to suit)将查看最开始的实体,然后将表单的提交绑定到该实体。如果实体存在,则会更新。如果没有,它将被保存为新实体。