如果您在控制器中,您只需这样做
$article->setAuthor($this->getUser());
$article->setUpdatedBy($this->getUser());
或者
如果你想要这是自动的
您需要在 Doctrine 事件上声明一个侦听器。在您的情况下,我猜测 preUpdate 包括当前用户。
这里有一个非常好的文档 http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html
我在这里编辑以回答您的评论
当您将侦听器声明为服务时,请不要担心您可以注入用户实体
例如 :
services:
your_listener:
class: App\AppBundle\Your_Listener
arguments: ["@security.token_storage"]
你的听众:
private $current_user;
public function __construct($security_context) {
if ($security_context->getToken() != null) {
$this->current_user = $security_context->getToken()->getUser();
}
}
现在你可以做
$entity= $args->getEntity(); // get your Article
if (!$entity instanceof Article) {
return;
}
$entity->setAuthor($this->current_user);
$entity->setUpdatedBy($this->current_user);