Symfony2:在实体类中获取security.context [英] Symfony2 : get security.context inside entity class

查看:75
本文介绍了Symfony2:在实体类中获取security.context的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在实体类中获取 security.context

Is it possible to get security.context inside an entity class?

我知道以下内容不会吗?工作。我不知道如何实现 $ user 部分。

I know the following doesn't work. I don't know how to implement the $user part.

/**
 * Set createdAt
 *
 * @ORM\PrePersist
 */
public function setCreatedAt()
{
    $user = $this->get('security.context')->getToken()->getUser();

    $this->owner = $user->getId();
    $this->createdAt = new \DateTime();
    $this->updatedAt = new \DateTime();
}


推荐答案

日期时间:

对于时间戳,您最好使用@hasLifeCycleCallback批注以及标记为@prePersist和@preUpdate的一些其他方法。
对于创建的您甚至可以只在__constructor本身中完成。

For the timestamps you are probably best of using the @hasLifeCycleCallback annotation and some additional methods marked as @prePersist and @preUpdate. For created you could even just do it in the __constructor itself.

请注意,必须将@preUpdate用于$ updatedAt属性,@ prePersist仅用于新实体。

Note that you must use @preUpdate for the $updatedAt property, @prePersist is only for new entities.

如果您有很多需要此实体的实体,则可以考虑使用Doctrine2侦听器以防止重复代码。

If you have a lot of entities that need this, you might consider a Doctrine2 listener to prevent repeated code.

对于所有权属性:

如果您始终希望将实体的所有权设置为当前登录的用户,则最好使用Doctrine2侦听器或订户。
这样,您不必将此逻辑添加到控制器中,也无需在任何需要创建实体的地方添加。

If you always want to set the ownership of an entity to the "currently logged in user", you are probably best of using a Doctrine2 listener or subscriber. This way you don't have to add this logic to your controller, or wherever you need to create an entity.

http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html
http://docs.doctrine- project.org/projects/doctrine-orm/zh-CN/latest/reference/events.html#onflush

创建记录的侦听器服务,请确保它使用构造函数获取安全上下文。
将其设置为onFlush事件,这样您可以在实际保存之前调整实体。这是处理此类事件的最佳方法。

Create a listener-service as documented, make sure it gets the security-context using the constructor. Set it to the event onFlush, this way you can adjust the entity just prior to actual saving. This is the best event to handle this kind of thing.

请确保您遵循Doctrine文档的onFlush一章中的脚注,否则将获得最后的更改

Make sure you follow the footnotes in the onFlush chapter on the Doctrine documentation, otherwise the last-minute-change won't be picked up by Doctrine.

您的听众可能看起来像这样:

Your listener should probably look something like:

class FlushExampleListener
{
    public function onFlush(OnFlushEventArgs $eventArgs)
    {
        $em = $eventArgs->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            if ($entity instanceof YourClass) {
                // change owner
                $entity->setOwner($this->securityContext->getToken()->getUser());
                // tell doctrine you changed it
                $uow->recomputeSingleEntityChangeSet($em->getClassMetadata(get_class($entity)), $entity);
            }
        }

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            if ($entity instanceof YourClass) {
                // change owner
                $entity->setOwner($this->securityContext->getToken()->getUser());
                // tell doctrine you changed it
                $uow->recomputeSingleEntityChangeSet($em->getClassMetadata(get_class($entity)), $entity);
            }
        }
    }
}

请请注意,这不会检查用户是否实际登录...

Please note that this does not check if a user is actually logged in...

这篇关于Symfony2:在实体类中获取security.context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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