2

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;
}

1 Answers1

0

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();
Brice
  • 1,026
  • 9
  • 20
  • Except he is trying to get it inside of an entity. – Cerad Feb 06 '14 at 13:40
  • @Brice ok thank you, sorry for the confusion. So, If I really need to have that information inside my controller, I should do an arrayCorrelation, isn't it? – user3279754 Feb 06 '14 at 13:47
  • What is the entity with `protected function getUploadDir()`? – Brice Feb 06 '14 at 13:47
  • @Brice It's an entity intended to upload a file. It's quite similar the one in the [official documentation](http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html#basic-setup) – user3279754 Feb 06 '14 at 13:53
  • Then I think you should add a `OneToMany` relation from `User` to `Document` in order, from `Document`, to retrieve the `User` that owns the `Document`. Or if it's not the kind of schema you want, then you should set the `path` to `Document` and use it in `getUploadDir()`. – Brice Feb 06 '14 at 13:57