1

I'm using Symfony2 Framework, and I want to update an entity with data from a form.

I'm using the same controller to populate the form with some data, and at the same time I'm using it to make the update query in the database. If I use $em->persist($foo) it does save exactly what I want, but I don't want to save it like it's new data, I want to update.

Reading symfony2 book, it says that $em->flush() is all we need so we can update.

I think I'm really close but of course I'm missing something.

Here's the code:

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));
}

So, need help with this please. :)

1 Answer 1

5

In your example, you're creating a new entity to start with:

$curriculo = new Curriculittle();

Since this isn't a pre-existing entity (for example, one you've retrieved via a database query), calling $em->persist($curriculo) followed by $em->flush() will save this entity as a new item. There is nothing to update, as the entity is new.

The same code for persisting and flushing can be used to update an existing entity; you just need to retrieve/obtain the existing entity first, instead of creating a new one. Currently it looks like you're doing this towards the end of the method:

$trabajador=$em->getRepository('SofLaSoflaBundle:Curriculittle')->find($id);

however you should do this prior to binding your form, eg:

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));
}

This example (adjust to suit) will look the entity up to start with, and then bind the form's submission to that entity. If the entity exists, it will be updated. If not, it will be saved as a new entity.

Sign up to request clarification or add additional context in comments.

1 Comment

Since Symfony2.3, you should using handleRequest instead of bindRequest symfony.com/doc/current/book/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.