I have my own custom User class implementing the UserInterface. A snippet follows:
class User implements UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=30, unique=true)
*/
protected $username;
...
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
...
In one controller, I want to be able to load the current logged in user. I do it as follows:
$user_id = NULL;
$securityContext = $this->container->get('security.context');
if ($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED'))
$user_obj = $this->get('security.context')->getToken()->getUser();
$user_id = $this->getUser()->getId(); //This yields an exception!
$user_obj->getUsername(); // This works!
}
I can do a getUsername(), it returns the adequate username. However, if I try a getId() it does not work. It yields the following exception:
Attempted to call method "getId" on class "Symfony\Component\Security\Core\User\User
Is there an easy way for me to be able to getId() from this object?