title: Symfony: How do I refresh the authenticated user from the database?
body: <p>Say for example I grant a new role to the currently authenticated user in a controller, like so:</p>
  
  <pre><code>$em = $this-&gt;getDoctrine()-&gt;getManager();
  $loggedInUser = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser();
  $loggedInUser-&gt;addRole('ROLE_XYZ');
  
  $em-&gt;persist($loggedInUser);
  $em-&gt;flush();
  </code></pre>
  
  <p>On the next page load, when I grab the authenticated user again:</p>
  
  <pre><code>$loggedInUser = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser();
  </code></pre>
  
  <p>They are not granted the role.  I am guessing this is because the user is stored in the session and needs to be refreshed.</p>
  
  <p>How do I do this?</p>
  
  <p>I am using FOSUserBundle if that makes a difference.</p>
  
  <blockquote>
    <p><strong>EDIT</strong>: This question was originally asked in the context of Symfony version 2.3 but there are answers for more recent versions below as well.</p>
  </blockquote>