我正在使用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));
}
Run Code Online (Sandbox Code Playgroud)
所以,请帮忙.:)
在您的示例中,您要创建一个新实体以开始:
$curriculo = new Curriculittle();
Run Code Online (Sandbox Code Playgroud)
因为这不是一个预先存在的实体(例如,你已经通过数据库查询检索一个),调用$em->persist($curriculo)
随后$em->flush()
将这个实体保存为一个新项目.没有更新,因为实体是新的.
用于持久化和刷新的相同代码可用于更新现有实体; 您只需要先检索/获取现有实体,而不是创建新实体.目前看起来你正在这个方法结束时这样做:
$trabajador=$em->getRepository('SofLaSoflaBundle:Curriculittle')->find($id);
Run Code Online (Sandbox Code Playgroud)
但是,您应该在绑定表单之前执行此操作,例如:
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));
}
Run Code Online (Sandbox Code Playgroud)
此示例(adjust to suit)将查看最初开始的实体,然后将表单的提交绑定到该实体.如果实体存在,则会更新.如果没有,它将被保存为新实体.
归档时间: |
|
查看次数: |
8056 次 |
最近记录: |