Get current logged in user in entity

爱⌒轻易说出口 提交于 2019-12-08 06:25:54

问题


I want to create some virtual properties for an entity in an n:m relation.

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
[...]                                                            [...]

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!

Edit: 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']

回答1:


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);
}

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

  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 } ]


来源:https://stackoverflow.com/questions/44243750/get-current-logged-in-user-in-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!