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
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;
}
//....
}
Is there any other way to retrieve it?