acid
acid

Reputation: 2149

Symfony2 Accessing User Entity Custom repository from Security Token

I have a User entity class, which is used for authentication provider:

/**
 * @ORM\Entity(repositoryClass="Aurora\LikeBundle\Entity\UserRepository")
 */
class User implements AdvancedUserInterface
{
    ...
}

In the UserRepository I have a custom method getServices(). In my controller, I can access the current user by calling $this->container->get('security.context')->getToken()->getUser() - but it returns only user object (with setters and getters) with no repository methods.

How can I access those from the security context?

Upvotes: 2

Views: 598

Answers (1)

Cyprian
Cyprian

Reputation: 11374

Respository isn't related to any particular Entity object. It's related to whole Entity class. So you have UserRepository for User entity but UserRepository for $user object doesn't have much sense.

If you want get repository for some entity (eg. an User entity), you can do it like this:

$repository = $this->getDoctrine()->getRepository('AuroraLikeBundle:User');

and just use it:

$repository->getServices(); // or
$repository->getServices($userId);

Upvotes: 4

Related Questions