How to get Container object from LifecycleEventArgs of postLoad in Entity?(如何从Entity中postLoad的LifecycleEventArgs获取Container对象?)
问题描述
我正在尝试使用 postLoad
方法.以下是我的代码.lifecycleCallbacks
将 Container 对象(在控制器中可用)注入到实体中.postLoad
方法的参数是 LifecycleEventArgs
.根据转储输出,我可以在 LifecycleEventArgs
的 EventManager
中看到容器属性(我想检索),但它似乎是私有属性并且没有 <EventManager
中的 code>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: IbwJobeetBundleEntityJob
tags:
- { name: doctrine.event_listener, event: postLoad }
IbwJobeetBundleEntityJob.php
IbwJobeetBundleEntityJob.php
<?php
namespace IbwJobeetBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineORMEventLifecycleEventArgs;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentHttpFoundationFileUploadedFile;
use SymfonyComponentDependencyInjectionContainerAware;
use SymfonyComponentDependencyInjectionContainerInterface;
use IbwJobeetBundleUtilsJobeet;
/**
* Job
*/
class Job
{
//....
/**
* @var Container
*/
protected $container;
public function postLoad(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
$entityManager = $eventArgs->getEntityManager();
$eventManager = $entityManager->getEventManager();
echo '<pre>';
DoctrineCommonUtilDebug::dump($eventManager, 3);
// want to get $eventManager->container here
exit;
}
//....
}
还有其他方法可以找回吗?
Is there any other way to retrieve it?
推荐答案
您可以使用 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: YourBundleDoctrineEventListenerJobListener
calls:
- [setContainer, ["@service_container"]]
tags:
- { name: doctrine.event_listener, event: postLoad }
现在容器被注入到你的监听器类的构造函数中:
Now the container is injected into the constructor of your listener class:
namespace YourBundleDoctrineEventListener;
use SymfonyComponentDependencyInjectionContainerInterface;
use DoctrineORMEventLifecycleEventArgs;
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.
这篇关于如何从Entity中postLoad的LifecycleEventArgs获取Container对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从Entity中postLoad的LifecycleEventArgs获取Container对象?
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01