I'm new to Symfony and following the Jobeet tutorial. I have three entities - Job, Category and User. I have the following service listener.
src/Ibw/JobeetBundle/Resources/config/services.yml
services:
ibw.jobeet.entity.job.container_aware:
class: Ibw\JobeetBundle\Doctrine\Event\Listener\JobListener
calls:
- [setContainer, ["@service_container"]]
tags:
- { name: doctrine.event_listener, event: postLoad }
src/Ibw/JobeetBundle/Doctrine/Event/Listener/JobListener.php
<?php
namespace Ibw\JobeetBundle\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;
}
/**
* @ORM\PostLoad
*/
public function postLoad(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if (method_exists($entity, 'setContainer')) {
$entity->setContainer($this->container);
}
}
}
I expected postLoad
would be invoked only for the Job entity, but I found that it is also called for the other two entities Category and User. I only define setContainer
in the Job entity. So, I got the undefined method for the other entities. My workaround is to check with method_exists
.
Is there any way to run postLoad
on a particular entity only?