Symfony2在实体存储库中获取用户ID [英] Symfony2 get user id in entity repository

查看:80
本文介绍了Symfony2在实体存储库中获取用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个页面,其中显示了系统的所有管理员。我想做的是自定义查询,以便将当前经过身份验证的用户从列表中排除。
现在,我知道我可以从控制器中获取 user_id 并将其传递给实体存储库,但是我想知道是否有一种方法可以通过实体存储库?

I have coded a page which displays all administrators of the system. What I want to do is customize my query so that it would exclude the currently authenticated user from the list.
Now I know I can get the user_id from the controller and pass it to the entity repository, but I was wondering if there is a way to access that directly through the Entity Repository?

例如:

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

class AdminUserRepository extends EntityRepository implements UserProviderInterface
{
   public function getAdmins($int = 10, $offset = 0, array $orderBy = array('admin_id', 'asc')){
        $admin_id = fetch the admin id ?;
        $query = $this->createQueryBuilder('admins')
            ->where("admins.admin_id != '".$admin_id."'")
            ->orderBy('admins.'.$orderBy[0], $orderBy[1])
            ->setFirstResult($offset)
            ->setMaxResults($int)
            ->getQuery()
            ->getResult();
        return $query;
   }
}


推荐答案

所以,您有一个当前的用户ID(例如, $ currentUserId ),并且您要为所有ID为 not 等于 $ currentUserId

So, you have a current User ID (let's say, $currentUserId), and you want to write a query for all Users with an ID that is not equal to $currentUserId?

尝试以下操作:

class SomeController
{
    public function someAction()
    {
        ...

        $qb = $userRepository->createQueryBuilder("user")
            ->where("user.id != ?1")
            ->setParameter(1, $currentUserId);

        $usersExcludingCurrent = $qb->getQuery()->getResult();

    ...
    }
}

编辑

啊,我明白你在说什么...

Ahh, I see what you're saying...

好吧,存储库实际上应该对自身之外的任何事物都不了解。也就是说,它不是容器感知的,因此不应该知道当前用户是谁。

Well, the repository really should be blind to anything outside of itself. That is, it's not container-aware, and should therefore not know who the current user is.

我的建议是为您的存储库提供一个功能,该功能可以获取除以下内容以外的所有内容特定的用户或用户ID。像这样的东西:

My suggestion would be to give your repository a function that gets everything except for a particular User or User ID. Something like:

class AdminUserRepository extends EntityRepository implements UserProviderInterface
{
    public function getAllAdminsExcept($userId, $int = 10, $offset = 0, array $orderBy = array('admin_id', 'asc'))
        {
            ...
        }
}

然后将当前用户逻辑放入控制器。您甚至可以定义可以访问 @security @doctrine 的服务,并将所有逻辑存储在其中。您的电话,但我想说您绝对应该使您的存储库不知道安全服务中正在发生的任何事情。

And then put the "current User" logic into your controller. You could even define a service that has access to both @security and @doctrine, and house all the logic there. Your call, but I'd say that you should definitely keep your repository unaware of anything going on in the security service.

这篇关于Symfony2在实体存储库中获取用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆