I have the following code:
public function editAction(Request $request)
{
$user = $this->get('security.context')->getToken()->getUser();
// Get the user
$user2 = $this->getDoctrine()
->getRepository('OpinionsUserBundle:User')
->findOneById($user->id);
echo $user->email . '<br>'; // Echo me@example.org
echo $user2->email . '<br>'; // Echo me@example.org
$user2->email = 'blah';
echo $user->email; // Echoes blah
die();
}
So I know that Doctrine must be doing something with references. The problem is I have a form where the user can change their name and email, but if the email is already in use I want to show an error. However, Symfony binds data to the user object when I check validation, so somehow the session is being updated with the new user object, logging me out or changing my user.
How can I avoid this?
The solution I ended up using was refreshing the user model (returning it to it’s original state) if my form validation failed.