如何从实体中的postLoad的LifecycleEventArgs获取容器对象? [英] How to get Container object from LifecycleEventArgs of postLoad in Entity?
问题描述
我正在尝试使用postLoad
lifecycleCallbacks
将Container对象(在控制器中可用)注入到Entity中. postLoad
方法的参数为LifecycleEventArgs
.根据转储输出,我可以在LifecycleEventArgs
的EventManager
中看到容器属性(我想检索),但是它似乎是私有属性,并且在EventManager
中没有任何getContainer()
方法.下面是我的代码.
I'm trying to inject the Container object (which is available in controllers) into an Entity using postLoad
lifecycleCallbacks
. The argument to the postLoad
method is LifecycleEventArgs
. I could see the container property (which I want to retrieve) in EventManager
of LifecycleEventArgs
according to the dump output, but it seems to be a private property and there is no getContainer()
method in EventManager
. The below is my code.
service.yml
service.yml
services:
ibw.jobeet.entity.job.container_aware:
class: Ibw\JobeetBundle\Entity\Job
tags:
- { name: doctrine.event_listener, event: postLoad }
Ibw \ JobeetBundle \ Entity \ Job.php
Ibw\JobeetBundle\Entity\Job.php
<?php
namespace Ibw\JobeetBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Ibw\JobeetBundle\Utils\Jobeet;
/**
* Job
*/
class Job
{
//....
/**
* @var Container
*/
protected $container;
public function postLoad(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
$entityManager = $eventArgs->getEntityManager();
$eventManager = $entityManager->getEventManager();
echo '<pre>';
\Doctrine\Common\Util\Debug::dump($eventManager, 3);
// want to get $eventManager->container here
exit;
}
//....
}
还有其他检索方法吗?
推荐答案
您可以使用setter-injection方法,从而在创建容器时将预定义方法(在本例中为setContainer()
)作为容器调用监听器服务:
You can use setter-injection which result in a call to a predefined method (setContainer()
in this case) with the container as an argument upon creation of the listener service:
services:
ibw.jobeet.entity.job.container_aware:
class: Your\Bundle\Doctrine\Event\Listener\JobListener
calls:
- [setContainer, ["@service_container"]]
tags:
- { name: doctrine.event_listener, event: postLoad }
现在,将容器注入到您的侦听器类的构造函数中:
Now the container is injected into the constructor of your listener class:
namespace Your\Bundle\Doctrine\Event\Listener;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
class JobListener
{
/** @var ContainerInterface */
protected $container;
/**
* @param ContainerInterface @container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}
public function postLoad(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
// do something with your entity here i.e.
$entity->setFoo($this->container->getParameter('foo'));
这只是一个例子.请考虑只注入您真正需要的服务-而不是注入容器本身.您将获得更好的可测试性和更好的性能.
This is just an example. Please consider injecting only the services you really need - instead of injecting the container itself. You will be rewarded with better testability and performance.
这篇关于如何从实体中的postLoad的LifecycleEventArgs获取容器对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!