将SecurityContext注入Symfony2中的监听器prePersist或preUpdate以获取createBy或updatedBy中的User会导致循环引用错误

发布于 2024-12-07 05:00:09 字数 1236 浏览 2 评论 0 原文

我设置了一个侦听器类,在其中我将在任何 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”。

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

I setup a listener class where i'll set the ownerid column on any doctrine prePersist. My services.yml file looks like this ...

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

and my class looks like this ...

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();

}
}

The result of this is the following error.

ServiceCircularReferenceException: Circular reference detected for service "doctrine.orm.default_entity_manager", path: "doctrine.orm.default_entity_manager -> doctrine.dbal.default_connection -> my.listener -> security.context -> security.authentication.manager -> fos_user.user_manager".

My assumption is that the security context has already been injected somewhere in the chain but I don't know how to access it. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我纯我任性 2024-12-14 05:00:09

我遇到了类似的问题,唯一的解决方法是在构造函数中传递整个容器(参数:['@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');

        // ...
    }
}

I had similar problems and the only workaround was to pass the whole container in the constructor (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');

        // ...
    }
}
最偏执的依靠 2024-12-14 05:00:09

从 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());
    }
}

对于一个不错的 create_by 示例,您可以使用 https://github.com/hostnet/entity-blamable-component/blob/master/src/Listener/BlamableListener.php 获取灵感。它使用 hostnet/entity-tracker-component 提供一个特殊事件,当您的请求期间实体发生更改时会触发该事件。 Symfony2 中还有一个用于配置此功能的捆绑包

As of Symfony 2.6 this issue should be fixed. A pull request has just been accepted into the master. Your problem is described in here.
https://github.com/symfony/symfony/pull/11690

As of Symfony 2.6, you can inject the security.token_storage into your listener. This service will contain the token as used by the SecurityContext in <=2.5. In 3.0 this service will replace the SecurityContext::getToken() altogether. You can see a basic change list here: http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements#deprecated-the-security-context-service

Example usage in 2.6:

Your configuration:

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

Your Listener

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());
    }
}

For a nice created_by example, you can use https://github.com/hostnet/entity-blamable-component/blob/master/src/Listener/BlamableListener.php for inspiration. It uses the hostnet/entity-tracker-component which provides a special event that is fired when an entity is changed during your request. There's also a bundle to configure this in Symfony2

物价感观 2024-12-14 05:00:09

该线程中已经有一个很好的答案,但一切都发生了变化。现在 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}

There's a great answer already in this thread but everything changes. Now there're entity listeners classes in Doctrine:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners-class

So you can add an annotation to your entity like:

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

And create a class like this:

class PhotoListener
{        
    private $container;

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

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

Also you should define this listener in services.yml like that:

photo_listener:
  class: App\Entity\Listener\PhotoListener
  public: false
  autowire: true
  tags:
    - {name: doctrine.orm.entity_listener}
眼睛会笑 2024-12-14 05:00:09

我使用教义配置文件来设置 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 来保留该方法并从中扩展其他实体。希望有帮助!

I use the doctrine config files to set preUpdate or prePersist methods:

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]

And the methods are declared in the entity, this way you don't need a listener and if you need a more general method you can make a BaseEntity to keep that method and extend the other entites from that. Hope it helps!

浅唱々樱花落 2024-12-14 05:00:09

Symfony 6.2.4

将其添加到您的实体中:

#[ORM\EntityListeners(["App\Doctrine\MyListener"])]

将其添加到您的 services.yaml 中:

App\Doctrine\MyListener:
    tags: [doctrine.orm.entity_listener]

然后您可以执行此操作:

<?php

namespace App\Doctrine;

use App\Entity\MyEntity;
use Symfony\Component\Security\Core\Security;

class MyListener
{
    private $security;

    public function __construct(Security $security)
    {
        $this->security = $security;
    }

    public function prePersist(MyEntity $myEntity)
    {
        //Your stuff   
    }
}

希望它有帮助。

Symfony 6.2.4

Add this in your Entity :

#[ORM\EntityListeners(["App\Doctrine\MyListener"])]

Add this in your services.yaml:

App\Doctrine\MyListener:
    tags: [doctrine.orm.entity_listener]

Then you can do this :

<?php

namespace App\Doctrine;

use App\Entity\MyEntity;
use Symfony\Component\Security\Core\Security;

class MyListener
{
    private $security;

    public function __construct(Security $security)
    {
        $this->security = $security;
    }

    public function prePersist(MyEntity $myEntity)
    {
        //Your stuff   
    }
}

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文