Php 预版本当前用户
[设置]Php 预版本当前用户,php,symfony,fosuserbundle,Php,Symfony,Fosuserbundle,[设置] Symfony 3.4 已登录用户(FosUserBundle) Projet实体(如下) src/AppBundle/Entity/Projet.php [问题] 现在,我正在PrePersit/PreUpdate上设置创建和修改。 我还想设置当前登录用户的id,我该怎么做?我认为您必须在订阅者中执行此操作,监听prepersist和preupdate事件。 通过注入security.token_存储服务,您将能够获得currentn用户我认为您必须在订户中执行此操作,监听pre
- Symfony 3.4
- 已登录用户(FosUserBundle)
实体(如下)Projet
PrePersit
/PreUpdate
上设置创建
和修改
。我还想设置当前登录用户的id,我该怎么做?我认为您必须在订阅者中执行此操作,监听prepersist和preupdate事件。
通过注入security.token_存储服务,您将能够获得currentn用户我认为您必须在订户中执行此操作,监听prepersist和preupdate事件。
您将能够通过注入security.token\u存储服务来获取currentn用户。为此,您需要访问
security.token\u存储服务,并调用$tokenStorage->getToken()->getUser()
来检索当前用户。由于实际上不建议将服务注入到实体中,因此应创建实体侦听器,如中所述
然后,通过将实体侦听器声明为服务并将security.token\u storage
添加到其构造函数参数,如下所示(在服务.yml
中):
您可以在prePersist
和preUpdate
方法中访问当前登录的用户
编辑:以下是您的听众的样子
AppBundle/Listener/ProjetListener.php
为此,您需要访问security.token\u storage
服务,并调用$tokenStorage->getToken()->getUser()
检索当前用户。由于实际上不建议将服务注入到实体中,因此应创建实体侦听器,如中所述
然后,通过将实体侦听器声明为服务并将security.token\u storage
添加到其构造函数参数,如下所示(在服务.yml
中):
您可以在prePersist
和preUpdate
方法中访问当前登录的用户
编辑:以下是您的听众的样子
AppBundle/Listener/ProjetListener.php
你把东西放在哪里?你不能在同一个地方获取用户实体,并在一个边注上使用那个用户的ID,考虑你为生命周期事件设置的行为,用于<代码>创建/>代码>和<代码>修改< /代码> DATE-TIME字段,也许你应该考虑使用一个简单注释来处理这个问题。你在哪里设置了什么?你不能在同一个地方获取用户实体,并在一个边注释上使用那个用户的ID,考虑到你为生命周期事件建立的行为,用于<代码>创建/>代码>和<代码>修改< /COD> DATE-TIME字段,也许你应该考虑使用一个简单的注释来处理这个问题。这应该是这样吗?@确切地说,您应该期望LifecycleEventArgs
NotPrePersist
。我在我的帖子中添加了你需要的听众的骨架。哦,我明白了。。很接近。。。我还可以移动此侦听器中日期的prePersist/preUpdate吗@是的,实际上你应该。对于那些领域,正如我在其他评论中所指出的,你可以考虑使用它。确切地说,您应该期望LifecycleEventArgs
NotPrePersist
。我在我的帖子中添加了你需要的听众的骨架。哦,我明白了。。很接近。。。我还可以移动此侦听器中日期的prePersist/preUpdate吗@是的,实际上你应该。对于这些字段,如我的其他注释中所指出的,您可以考虑使用。
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);
}
}
}
listener.projet:
class: AppBundle\Listener\ProjetListener
arguments: [ "@security.token_storage" ]
tags:
- { name: doctrine.orm.entity_listener, lazy: true }
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 .. */
}
}