I am new to Symfony and follow the Jobeet tutorial . I have three objects - Job, Category and User. I have the following service listener.
CSI / IVT / JobeetBundle / Resources / configurations / 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 / IVT / 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
{
protected $container;
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}
public function postLoad(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if (method_exists($entity, 'setContainer')) {
$entity->setContainer($this->container);
}
}
}
I expected it to postLoadbe called only for the Job object , but I found that it is also called for the other two objects, Category and User . I define only setContainerin the Job object . So, I got the undefined method for other objects. My workaround is to check with method_exists.
postLoad ?