Getting "cannot refresh user" error with symfony2 and doctrine

"You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine."

This is not the answer to the question, but if someone googles the error message above (like I did) this is the top hit. And since I found my solution, I thought I'd share.

You will get this error if you remove the current user from the database and then try to redirect the user. (Which might seem like a silly mistake, but the error message sure isn't helping!) The solution is to simply clear out the session before you redirect.

$this->get('security.context')->setToken(null);
$this->get('request')->getSession()->invalidate();

From the code of EntityUserProvider.php it seems that you have to serialize id of the user also.