I setup a listener class where i'll set the ownerid column on any doctrine prePersist. My services.yml file looks like this ...
我设置了一个监听器类,我将在任何doctrine prePersist上设置ownerid列。我的services.yml文件看起来像这样......
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".
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”。
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?
我的假设是安全上下文已经被注入链中的某个地方,但我不知道如何访问它。有任何想法吗?
4 个解决方案
#1
68
I had similar problems and the only workaround was to pass the whole container in the constructor (arguments: ['@service_container']
).
我有类似的问题,唯一的解决方法是在构造函数中传递整个容器(参数:['@ 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');
// ...
}
}
#2
35
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
从Symfony 2.6开始,这个问题应该修复。拉出请求刚刚被主人接受。你的问题在这里描述。 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
从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
Example usage in 2.6:
2.6中的示例用法:
Your configuration:
你的配置:
services:
my.listener:
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
对于一个很好的created_by示例,您可以使用https://github.com/hostnet/entity-blamable-component/blob/master/src/Listener/BlamableListener.php获取灵感。它使用hostnet / entity-tracker-component,它提供在请求期间更改实体时触发的特殊事件。还有一个捆绑包可以在Symfony2中配置它
- https://github.com/hostnet/entity-tracker-component
- https://github.com/hostnet/entity-tracker-component
- https://github.com/hostnet/entity-tracker-bundle
- https://github.com/hostnet/entity-tracker-bundle
#3
0
I use the doctrine config files to set preUpdate
or prePersist
methods:
我使用doctrine配置文件来设置preUpdate或prePersist方法:
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!
并且方法在实体中声明,这样您就不需要监听器,并且如果您需要更通用的方法,您可以使BaseEntity保留该方法并从中扩展其他entite。希望能帮助到你!
#4
0
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
在这个帖子中已经有了一个很好的答案,但一切都在改变。现在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:
你也应该在services.yml中定义这个监听器:
photo_listener:
class: App\Entity\Listener\PhotoListener
public: false
autowire: true
tags:
- {name: doctrine.orm.entity_listener}
#1
68
I had similar problems and the only workaround was to pass the whole container in the constructor (arguments: ['@service_container']
).
我有类似的问题,唯一的解决方法是在构造函数中传递整个容器(参数:['@ 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');
// ...
}
}
#2
35
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
从Symfony 2.6开始,这个问题应该修复。拉出请求刚刚被主人接受。你的问题在这里描述。 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
从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
Example usage in 2.6:
2.6中的示例用法:
Your configuration:
你的配置:
services:
my.listener:
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
对于一个很好的created_by示例,您可以使用https://github.com/hostnet/entity-blamable-component/blob/master/src/Listener/BlamableListener.php获取灵感。它使用hostnet / entity-tracker-component,它提供在请求期间更改实体时触发的特殊事件。还有一个捆绑包可以在Symfony2中配置它
- https://github.com/hostnet/entity-tracker-component
- https://github.com/hostnet/entity-tracker-component
- https://github.com/hostnet/entity-tracker-bundle
- https://github.com/hostnet/entity-tracker-bundle
#3
0
I use the doctrine config files to set preUpdate
or prePersist
methods:
我使用doctrine配置文件来设置preUpdate或prePersist方法:
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!
并且方法在实体中声明,这样您就不需要监听器,并且如果您需要更通用的方法,您可以使BaseEntity保留该方法并从中扩展其他entite。希望能帮助到你!
#4
0
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
在这个帖子中已经有了一个很好的答案,但一切都在改变。现在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:
你也应该在services.yml中定义这个监听器:
photo_listener:
class: App\Entity\Listener\PhotoListener
public: false
autowire: true
tags:
- {name: doctrine.orm.entity_listener}