Symfony2:SonataAdminBundle – 如何在管理类中获取代表当前用户的对象?

栏目: PHP · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/22069541/symfony2-sonataadminbundle-how-can-i-get-the-object-representing-the-current

我使用sonata-admin包.

我在PageEntity中与用户(FOSUserBundle)建立了关系.

我想保存创建或更改页面的当前用户.

我的猜测是获取admin类的postUpdate和postPersist方法中的用户对象,并且此对象在setUser方法中传输.

但是如何实现呢?

在google的小组中,我看到了

public function setSecurityContext($securityContext) {
        $this->securityContext = $securityContext;
    }

    public function getSecurityContext() {
        return $this->securityContext;
    }

    public function prePersist($article) {
        $user = $this->getSecurityContext()->getToken()->getUser();

        $appunto->setOperatore($user->getUsername());
    }

但这不起作用

在admin类中,您可以像这样获取当前登录的用户:

$this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser()

编辑基于反馈

而你这样做呢?因为这应该有效.

/**
     * {@inheritdoc}
     */
    public function prePersist($object)
    {

$user = $this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser();
        $object->setUser($user);
    }

    /**
     * {@inheritdoc}
     */
    public function preUpdate($object)
    {
$user = $this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser();
        $object->setUser($user);
    }

翻译自:https://stackoverflow.com/questions/22069541/symfony2-sonataadminbundle-how-can-i-get-the-object-representing-the-current


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Agile Web Development with Rails, Third Edition

Agile Web Development with Rails, Third Edition

Sam Ruby、Dave Thomas、David Heinemeier Hansson / Pragmatic Bookshelf / 2009-03-17 / USD 43.95

Rails just keeps on changing. Rails 2, released in 2008, brings hundreds of improvements, including new support for RESTful applications, new generator options, and so on. And, as importantly, we’ve a......一起来看看 《Agile Web Development with Rails, Third Edition》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具