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

  • Home
  • SEARCH
  • 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 6208373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:47:50+00:00 2026-05-24T05:47:50+00:00

Can anyone please show me a specific example of a Symfony2 form entity update?

  • 0

Can anyone please show me a specific example of a Symfony2 form entity update? The book only shows how to create a new entity. I need an example of how to update an existing entity where I initially pass the id of the entity on the query string.

I’m having trouble understanding how to access the form again in the code that checks for a post without re-creating the form.

And if I do recreate the form, it means I have to also query for the entity again, which doesn’t seem to make much sense.

Here is what I currently have but it doesn’t work because it overwrites the entity when the form gets posted.

public function updateAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();
    $testimonial = $em->getRepository('MyBundle:Testimonial')->find($id);
    $form = $this->createForm(new TestimonialType(), $testimonial);

    $request = $this->get('request');
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        echo $testimonial->getName();

        if ($form->isValid()) {
            // perform some action, such as save the object to the database
            //$testimonial = $form->getData();
            echo 'testimonial: ';
            echo var_dump($testimonial);
            $em->persist($testimonial);
            $em->flush();

            return $this->redirect($this->generateUrl('MyBundle_list_testimonials'));
        }
    }

    return $this->render('MyBundle:Testimonial:update.html.twig', array(
        'form' => $form->createView()
    ));
}
  • 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-24T05:47:52+00:00Added an answer on May 24, 2026 at 5:47 am

    Working now. Had to tweak a few things:

    public function updateAction($id)
    {
        $request = $this->get('request');
    
        if (is_null($id)) {
            $postData = $request->get('testimonial');
            $id = $postData['id'];
        }
    
        $em = $this->getDoctrine()->getEntityManager();
        $testimonial = $em->getRepository('MyBundle:Testimonial')->find($id);
        $form = $this->createForm(new TestimonialType(), $testimonial);
    
        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);
    
            if ($form->isValid()) {
                // perform some action, such as save the object to the database
                $em->flush();
    
                return $this->redirect($this->generateUrl('MyBundle_list_testimonials'));
            }
        }
    
        return $this->render('MyBundle:Testimonial:update.html.twig', array(
            'form' => $form->createView()
        ));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can anyone please show me an example about using regex (regex.h) in C/C++ to
can anyone please suggest a good code example of vb.net/c# code to put the
can anyone please let me know is it possible to show error provider icon
Here I am again asking for help. Can anyone please show me a sample
Can anyone please provide or show me how to download a PDF asynchronously if
Can anyone please help me in finding good tutorials for php/jquery/ajax form submission validating
Can anyone please why does this javascript work only in firefox? function filter(obj, what,
Can anyone please help me with this. I am new to css styling. I
Can anyone please tell me if Certifying Authorities (CAs) are allowed to make modifications
Can anyone please provide a link to download the .NET 3.5 SDK? I checked

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.