PrePersit当前用户 [英] PrePersit current user

查看:118
本文介绍了PrePersit当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[设置]

  • Symfony 3.4
  • 登录用户(FosUserBundle)
  • Projet实体(如下)
  • Symfony 3.4
  • Logged user (FosUserBundle)
  • Projet Entity (below)

src/AppBundle/Entity/Projet.php

class Projet {
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORM\Column(name="titre", type="string", length=50)
     */
    private $titre;
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="creation", type="datetime")
     */
    private $creation;
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="modification", type="datetime")
     */
    private $modification;
    /**
     * @var
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="projet")
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function updatedTimestamps() {
        $this->setModification(new \DateTime());
        if($this->getCreation() == null) {
            $this->setCreation(new \DateTime());
            $this->setSupprime(false);
        }
    }
}

[问题]

就这样,我要在PrePersit/PreUpdate上设置creationmodification.
我还要设置当前登录用户的ID,该怎么办?

As is, I'm setting creation and modification on PrePersit/PreUpdate.
I would like to also set the id of the current logged user, how can I do it?

推荐答案

为此,您需要有权访问security.token_storage服务并调用$tokenStorage->getToken()->getUser()来检索当前用户.由于实际上不建议将服务注入实体,因此您应该按照

To do this, you need to have access to the security.token_storage service and call $tokenStorage->getToken()->getUser() to retrieve the current user. Since it is not really recommended to inject a service into an entity, you should create an entity listener as described in this section of the doctrine documentation.

然后,通过将实体侦听器声明为服务并将security.token_storage添加到其构造函数参数中(在您的services.yml中):

Then, by declaring your entity listener as a service and adding the security.token_storage to its constructor arguments like this (in your services.yml):

listener.projet:
    class: AppBundle\Listener\ProjetListener
    arguments: [ "@security.token_storage" ]
    tags:
        - { name: doctrine.orm.entity_listener, lazy: true }

您将可以在prePersistpreUpdate方法中访问当前登录的用户.

You would be able to access the currently logged in user inside your prePersist and preUpdate methods.

编辑:这是您的听众的样子

Here is how your listener should look like

AppBundle/Listener/ProjetListener.php

namespace AppBundle\Listener;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use AppBundle\Entity\Projet;

class ProjetListener
{
    private $tokenStorage;

    public function __construct(TokenStorage $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    public function prePersist(Projet $projet, LifecycleEventArgs $args)
    {
        // Assigning the current user to the Projet instance being persisted
        $projet->setUser($this->tokenStorage->getToken()->getUser());
        /* .. other actions of prePersist .. */
    }

    public function preUpdate(Projet $projet, PreUpdateEventArgs $args)
    {
        /* .. actions of preUpdate .. */
    }

}

这篇关于PrePersit当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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