问题描述
[设置]
- Symfony 3.4
- 记录的用户(fosuserbundle)
- Projet实体(下图)
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); } } }
[问题]
我正在设置creation和modification on PrePersit/PreUpdate.
我还想设置当前记录用户的ID,我该怎么做?
推荐答案
要这样做,您需要访问security.token_storage服务并调用$tokenStorage->getToken()->getUser()才能检索当前用户.由于不建议将服务注入实体,因此您应该按照学说文档的这一部分.
然后,通过将您的实体侦听器声明为服务,并将security.token_storage添加到其构造函数参数(在您的services.yml中):
listener.projet: class: AppBundle\Listener\ProjetListener arguments: [ "@security.token_storage" ] tags: - { name: doctrine.orm.entity_listener, lazy: true }
您将能够访问prePersist和
编辑:这是您的听众应该看起来像
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 .. */ } }
其他推荐答案
我认为您必须在订阅者中进行此操作,聆听预科和预言学说事件. 您将能够通过注入security.token_storage服务
来获得当前用户问题描述
[SETTINGS]
- 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); } } }
[PROBLEM]
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?
推荐答案
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.
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 }
You would be able to access the currently logged in user inside your prePersist and preUpdate methods.
Edit: 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 .. */ } }
其他推荐答案
I think you have to do this in a subscriber, listening on prepersist and preupdate doctrine event. You will be able to get the currentn user by injecting the security.token_storage service