Php 如何从实体中postLoad的LifecycleEventArgs获取容器对象?
我正在尝试使用Php 如何从实体中postLoad的LifecycleEventArgs获取容器对象?,php,symfony,entity,containers,symfony-2.6,Php,Symfony,Entity,Containers,Symfony 2.6,我正在尝试使用postLoadlifecycellbacks将容器对象(在控制器中可用)注入实体。postLoad方法的参数是LifecycleEventArgs。根据转储输出,我可以在LifecycleEventArgs的EventManager中看到container属性(我要检索),但它似乎是私有属性,EventManager中没有getContainer()方法。下面是我的代码 service.yml services: ibw.jobeet.entity.job.contain
postLoad
lifecycellbacks
将容器对象(在控制器中可用)注入实体。postLoad
方法的参数是LifecycleEventArgs
。根据转储输出,我可以在LifecycleEventArgs
的EventManager
中看到container属性(我要检索),但它似乎是私有属性,EventManager
中没有getContainer()
方法。下面是我的代码
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
<?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注入,这将导致在创建侦听器服务时调用预定义方法(setContainer()
),并将容器作为参数:
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'));
现在,容器被注入到侦听器类的构造函数中:
这只是一个例子。请考虑只注入您真正需要的服务,而不是注入容器本身。您将获得更好的可测试性和性能。我得到了这个错误服务“ibw.jobeet.entity.job.container\u aware”依赖于不存在的服务“container”。
我发现[“@service\u container”]
是正确的用法。@service\u container
是正确的-很抱歉很快就把它砍掉了。。。为将来的读者更新了答案。有时,postLoad
没有为存储库运行,在本例中为“作业”存储库。我认为加载存储库时会调用postLoad
。在某些情况下,存储库不会加载,例如,对于路由/job/new
。
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'));