phpsymfonyfosuserbundlepugxmultiuserbundle

Get user object inside an entity


I'm trying to get user object inside an entity. I'm using FosUserBundle and PugxMultiUserBundle, and I tried the following command:

container->get('fos_user.user_manager')->getToken()->getUser();

but I get:

Undefined property $container

How can I fix it?

Thank you for your help.

The function that doesn't work is inside an entity, and it is the following:

protected function getUploadDir()
{
    $userManager = $this->container->get('security.context');
    $user = $userManager->findUserByUsername($this->container->get('security.context')
                ->getToken()
                ->getUser());
    return 'uploads/'.$user;
}

Solution

  • UPDATE after @OP edited...

    You cannot access the container (hence the userManager) from an entity. The entity should not access the container itself.


    You are missing $ before the name of your variable.

    But that's not all, solution inside a controller :

    $userManager = $this->container->get('fos_user.user_manager');
    $user = $userManager->findUserByUsername($this->container->get('security.context')
                        ->getToken()
                        ->getUser())
    

    But, inside a controller, your should be able to get user by doing the following :

    $this->getUser();