将SecurityContext注入Symfony2中的Listener prePersist或preUpdate,以在createBy或updatedBy中获取User导致循环引用错误

时间:2011-09-26 20:37:28

标签: php doctrine-orm symfony

我设置了一个监听器类,我将在任何doctrine prePersist上设置ownerid列。我的services.yml文件看起来像这样......

services:
my.listener:
    class: App\SharedBundle\Listener\EntityListener
    arguments: ["@security.context"]
    tags:
        - { name: doctrine.event_listener, event: prePersist }

我的班级看起来像这样......

use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\Security\Core\SecurityContextInterface;

class EntityListener
{

protected $securityContext;

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


/**
 *
 * @param LifecycleEventArgs $args 
 */
public function prePersist(LifecycleEventArgs $args)
{

    $entity = $args->getEntity();
    $entityManager = $args->getEntityManager();

    $entity->setCreatedby();

}
}

结果是以下错误。

  

ServiceCircularReferenceException:为服务“doctrine.orm.default_entity_manager”检测到循环引用,路径:“doctrine.orm.default_entity_manager - > doctrine.dbal.default_connection - > my.listener - > security.context - > security .authentication.manager - > fos_user.user_manager“。

我的假设是安全上下文已经被注入链中的某个地方,但我不知道如何访问它。有任何想法吗?

4 个答案:

答案 0 :(得分:68)

我有类似的问题,唯一的解决方法是在构造函数(arguments: ['@service_container'])中传递整个容器。

use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface;

class MyListener
{
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    // ...

    public function prePersist(LifeCycleEventArgs $args)
    {
        $securityContext = $this->container->get('security.context');

        // ...
    }
}

答案 1 :(得分:36)

从Symfony 2.6开始,这个问题应该修复。拉取请求刚刚被主人接受。您的问题在此处描述。 https://github.com/symfony/symfony/pull/11690

从Symfony 2.6开始,您可以将security.token_storage注入您的监听器。此服务将包含SecurityContext在< = 2.5中使用的令牌。在3.0中,此服务将完全替换SecurityContext::getToken()。您可以在此处查看基本更改列表:http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements#deprecated-the-security-context-service

2.6中的用法示例:

您的配置:

services:
    my.entityListener:
        class: App\SharedBundle\Listener\EntityListener
        arguments:
            - "@security.token_storage"
        tags:
            - { name: doctrine.event_listener, event: prePersist }


您的听众

namespace App\SharedBundle\Listener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class EntityListener
{
    private $token_storage;

    public function __construct(TokenStorageInterface $token_storage)
    {
        $this->token_storage = $token_storage;
    }

    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $entity->setCreatedBy($this->token_storage->getToken()->getUsername());
    }
}


对于一个很好的created_by示例,您可以使用https://github.com/hostnet/entity-blamable-component/blob/master/src/Listener/BlamableListener.php获取灵感。它使用hostnet / entity-tracker-component,它提供在请求期间更改实体时触发的特殊事件。还有一个在Symfony2中配置它的包

答案 2 :(得分:1)

这个帖子里已经有了一个很好的答案但是一切都改变了。现在Doctrine中有实体监听器类: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners-class

因此,您可以为实体添加注释,如:

/**
 * @ORM\EntityListeners({"App\Entity\Listener\PhotoListener"})
 * @ORM\Entity(repositoryClass="App\Repository\PhotoRepository")
 */
class Photo 
{
    // Entity code here...
}

创建一个这样的类:

class PhotoListener
{        
    private $container;

    function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /** @ORM\PreRemove() */
    public function preRemoveHandler(Photo $photo, LifecycleEventArgs $event): void
    {
         // Some code here...
    }
}

此外,您应该在services.yml中定义此侦听器:

photo_listener:
  class: App\Entity\Listener\PhotoListener
  public: false
  autowire: true
  tags:
    - {name: doctrine.orm.entity_listener}

答案 3 :(得分:0)

我使用doctrine配置文件设置preUpdateprePersist方法:

Project\MainBundle\Entity\YourEntity:
    type: entity
    table: yourentities
    repositoryClass: Project\MainBundle\Repository\YourEntitytRepository
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO

    lifecycleCallbacks:
        prePersist: [methodNameHere]
        preUpdate: [anotherMethodHere]

这些方法在实体中声明,这样你就不需要一个监听器,如果你需要一个更通用的方法,你可以使BaseEntity保持该方法并从中扩展其他的方法。希望它有所帮助!