在 EntityRepository 中访问当前登录的用户 [英] Access currently logged in user in EntityRepository
问题描述
我想创建一个简单的博客示例,其中用户将最喜欢的类别附加到该帐户.这意味着只能为此类别撰写文章.(其中一些 - 主要是管理员 - 将有机会切换类别,但这不是问题......现在^^)
I want to create a simple blog example where users have a favourite category attached to there account. This means the can only write articles for this category. (Some of them - mostly admins -will get the opportunity to switch categories, but that's not the problem... for now ^^)
所以我首先创建了 User- 和 Category 实体之间的关系.一切正常.每个用户现在都选择了一个主要类别.
So I first created a relation between the User- and the Category entity. Everything works fine. Each user now has a main category selected.
唯一困扰我的是我无法在 EntityType (formbuilder) 和 EntityRepository 类中获取当前登录的用户.
Only thing which bothers me, is that I cant get hold of the current logged in user in the EntityType (formbuilder) and EntityRepository classes.
在我的新帖子"表单中,存在与其他实体(例如标签)的关系.我使用 EntityType 类中的实体"表单类型来生成这些表单元素.现在我不想过滤标签,只允许选择与当前登录的用户类别具有相同类别关系的标签.
In my "New Post" form there are relations to other entities (e.g. Tags). I use the 'entity' formtype in the EntityType class to generate these form elements. Now i wan't to filter the tags, to only allow tags which have the same category relation as the currently logged in users category to be selectable.
我尝试使用实体表单类型中的 query_builder 选项.但是由于我无法获取当前用户对象,我不知道他选择了哪个类别.EntityRepository 也有同样的问题.
I tried to use the query_builder option from the entity formtype. But as i can't get the current user object, I don't know which category he has selected. Same problem with the EntityRepository.
现在我可以过滤 PostController 中已有的标签,但问题是,我会一遍又一遍地需要它.因此,我不想每次添加新内容时都对此进行编码.
Now I could filter the tags already in the PostController but the problem is, that I will need this over and over again. And therefore I don't wan't to code this everytime I add something new.
我认为最好将此过滤器放在 EntityRepository 中.所以我总是可以访问 findAllByCategory.但我需要那里的用户对象.
I thought it would be the best to place this filter in the EntityRepository. So I can always access the findAllByCategory. But I need the user-object in there.
实现这一目标的最佳方法是什么?已经搜索了很多,但要么我搜索了错误的术语,要么没有人遇到这个问题:)
What is the best way to accomplish this? Have searched a lot, but either I searched for the wrong terms or no one has this problem :)
推荐答案
您可以在定义为服务的表单类型中注入安全上下文.然后在您的标签字段中使用带有 $user
(当前登录的用户)的查询构建器来过滤与当前登录的具有相同类别关系的标签:
You can inject security context in your form type defined as a service. Then in your tags field use the query builder with $user
(current logged user) to filter tags which have the same category relation as the currently logged:
/** @DI\Service("form.type.post") */
class PostType extends \Symfony\Component\Form\AbstractType
{
/**
* @var \Symfony\Component\Security\Core\SecurityContext
*/
protected $securityContext;
/**
* @DI\InjectParams({"securityContext" = @DI\Inject("security.context")})
*
* @param \Symfony\Component\Security\Core\SecurityContext $context
*/
public function __construct(SecurityContext $securityContext)
{
$this->securityContext = $securityContext;
}
public function buildForm(FormBuilder $builder, array $options)
{
$user = $this->securityContext()->getToken()->getUser();
$builder->add('tags', 'entity', array(
'label' => 'Tags',
'class' => 'Acme\HelloBundle\Entity\Tag',
'property' => 'name',
'query_builder' => function(EntityRepository $er) use($user) {
return $er->getAllSuitableForUserChoiceQueryBuilder($user);
},
'multiple' => true,
'expanded' => true,
}
}
将标签过滤到您的存储库中:
Filter tags into your repository:
class TagRepository extends EntityRepository
{
public function getAllSuitableForUserChoiceQueryBuilder(User $user)
{
// Filter tags that can be selected by a given user
}
}
这篇关于在 EntityRepository 中访问当前登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!