自定义存储库,这个用户在symfony2中

时间:2013-07-05 18:38:05

标签: symfony doctrine repository

如何从存储库中访问上下文用户(即:$ this-> getUser())?

类似的东西:

public function findInviteRequestByUser(\Fundacity\UserBundle\Entity\User $user)
{
    $contextUser = $this->getUser();   
    ...
    // HERE GOES THE QUERY
    ... 

    return $query->getResult();
}

1 个答案:

答案 0 :(得分:0)

您应该将安全上下文作为参数传递 你的控制器

$securityContext = $this->get('security.context');
$em->getRepository('YourBundle')->findInviteRequestByUser($securityContext);

您的实体存储库

public function findInviteRequestByUser(\Symfony\Component\Security\Core\SecurityContext $context)
{
    $user = $context->getToken()->getUser();   

    ... 
}