获取实体中的当前登录用户 [英] Get current logged in user in entity
问题描述
我想在 n:m
关系中为实体创建一些虚拟属性。
I want to create some virtual properties for an entity in an n:m
relation.
我有 User
, Achievment
和 AchievementUser
实体。用户在成就中拥有的值存储在实体 AchievementUser
中的字段 value
中。
I have an User
, an Achievment
and an AchievementUser
entity. The value an user have in an Achievement is stored in the field value
in the entity AchievementUser
.
User -------- 1:n -------- AchievementUser -------- n:1 -------- Achievement
name:String value:Integer name:String
[...] [...]
现在,我想使用成就本身返回用户在成就中拥有的值
。所以我需要一个虚拟属性和 Achievement
实体中的方法 getValue()
,但是要获取相应的 AchievementUser
对象,我需要当前登录用户的ID。
Now I want to return the value
an user have in an achievement with the achievement itself. So I need a virtual property and a method getValue()
in the Achievement
entity, but to get the corresponding AchievementUser
object, I need the ID of the current logged in user.
如何获取此信息?还是有其他可能获得成就的用户价值?谢谢您的帮助!
How can I get this? Or is there an other possiblility to get the user value for an achievement? Thanks for your help!
编辑:我只有一个基于API的应用程序。仅序列化程序执行Getter方法。这是我的序列化器文件的内容:
I only have an API based application. Only the serializer executes the Getter method. Here is the content of my serializer file:
virtual_properties:
getValue:
serialized_name: value
type: integer
groups: ['achievement_default']
推荐答案
您可以在Achievement实体中实现一个方法,并将当前经过身份验证的用户从twig模板的控制器传递到该方法中。
You can implement a method in the Achievement entity and pass the current authenticated user into it from your controller of twig template.
use Doctrine\Common\Collections\Criteria;
// ...
/**
* @return Collection
*/
public function getAchievementUsers(User $user)
{
$criteria = Criteria::create()->where(Criteria::expr()->eq('user', $user));
return $this->achievementUsers->matching($criteria);
}
如果使用JMS序列化程序,则可以添加虚拟字段并填充通过定义序列化侦听器并注入TokenStorage来检索当前经过身份验证的用户,使用getAchievementUsers方法将其与数据一起使用。
In case of using a JMS serializer, you can add a virtual field and fill it with data using getAchievementUsers method by defining a serialization listener and injecting the TokenStorage to retrieve the current authenticated user.
<?php
namespace AppBundle\Listener\Serializer;
...
use JMS\Serializer\GenericSerializationVisitor;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
class AchievementSerializerListener
{
/**
* @var User
*/
protected $currentUser;
/**
* @param TokenStorage $tokenStorage
*/
public function __construct(TokenStorage $tokenStorage)
{
$this->currentUser = $tokenStorage->getToken()->getUser();
}
/**
* @param ObjectEvent $event
*/
public function onAchievementSerialize(ObjectEvent $event)
{
if (!$this->currentUser) {
return;
}
/** @var Achievement $achievement */
$achievement = $event->getObject();
/** @var GenericSerializationVisitor $visitor */
$visitor = $event->getVisitor();
$visitor->setData(
'achievement_users',
$achievement->getAchievementUsers($this->currentUser)
);
}
}
services.yml
services.yml
app.listener.serializer.achievement:
class: AppBundle\Listener\Serializer\AchievementSerializerListener
arguments:
- '@security.token_storage'
tags: [ { name: jms_serializer.event_listener, event: serializer.post_serialize, class: AppBundle\Entity\Achievement, method: onAchievementSerialize } ]
这篇关于获取实体中的当前登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!