Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7190181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:27:34+00:00 2026-05-28T19:27:34+00:00

I’m using Symfony2 Framework, and I want to update an entity with data from

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:27:35+00:00Added an answer on May 28, 2026 at 7:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.