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 7681095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:15:51+00:00 2026-05-31T18:15:51+00:00

I am trying to add a simple form to allow my users to edit

  • 0

I am trying to add a simple form to allow my users to edit their profile. My problem is:

Since the entity “linked” to the form is the same as the current user object ($user === $entity, see below), if the form validation fails, then the view is rendered with the modified user object (ie. with values of the non-valid form).

Here my (classic) controller:

public function profileAction()
{
    $em = $this->getDoctrine()->getEntityManager();

    $user = $this->get('security.context')->getToken()->getUser();
    $entity = $em->getRepository('AcmeSecurityBundle:User')->find($user->id);
    // $user === $entity => true

    $form = $this->createForm(new ProfileType(), $entity);

    $request = $this->getRequest();

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

        if ($form->isValid()) {
            $em->persist($entity);
            $em->flush();

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

    return $this->render('AcmeSecurityBundle:User:profile.html.twig', array(
        'entity'      => $entity,
        'form'   => $form->createView(),
    ));
}

So I wondered how to have two distincts objects $user and $entity. I used clone() and it worked well for the view rendering part (the $user object was not modified), but it created a new record in database instead of updating the old one.

PS: I know I should use FOSUserBundle. But I would really like to understand my mistake here 🙂

  • 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-31T18:15:53+00:00Added an answer on May 31, 2026 at 6:15 pm

    I used the same solution as FOSUserBundle, which is calling $em->refresh() on my entity when the form validation fails:

    public function profileAction()
    {
        $em = $this->getDoctrine()->getEntityManager();
    
        $user = $this->get('security.context')->getToken()->getUser();
        $entity = $em->getRepository('AcmeSecurityBundle:User')->find($user->id);
    
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find User entity.');
        }
    
        $form = $this->createForm(new ProfileType(), $entity);
    
        $request = $this->getRequest();
    
        if ($request->getMethod() === 'POST')
        {
            $form->bindRequest($request);
    
            if ($form->isValid()) {
                $em->persist($entity);
                $em->flush();
    
                return $this->redirect($this->generateUrl('profile'));
            }
    
            $em->refresh($user); // Add this line
        }
    
        return $this->render('AcmeSecurityBundle:User:profile.html.twig', array(
            'entity'      => $entity,
            'form'   => $form->createView(),
        ));
    }
    

    Note that if you are using what is called a “virtual” field in “How to handle File Uploads with Doctrine” (in my case “picture_file” you will need to clear it by hand:

    $em->refresh($user);
    $user->picture_file = null; // here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to display a simple ModelForm for a user's profile and allow the
I'm trying to add a simple datepicker to my form and it is working
I'm trying to create a simple form to add companies and i've a trouble
I'm trying to add a generic simple function that shows/hides form field's associated hint
I have a form that I am trying to add a simple select element
I'm trying to add simple login page to ASP.NET MVC app. I actually use
I'm trying to add a very simple action to the context menu of Eclipse:
I'm trying to create a simple Add-On for SQL Server 2008; it is simply
I am trying to add a draggable object to to a simple html page.
This may be a simple answer, but I'm trying to add a dom-created element

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.