Reputation: 3833
I have a Symfony project with a lot of bundles.
In one of them, I have a standard Doctrine listener like this:
class MyListener
{
public function postLoad(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof MyEntity) {
//do something
}
...
Now I've created a new Bundle that also loads these Entities in a Controller.
As expected, it also triggers the postLoad in the listener.
I need it not to trigger it, or if it's triggered by this Bundle/Controller, to don't do anything, something like:
class MyListener
{
public function postLoad(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($caller = "DontTriggerBundle")
return true;
}
if ($entity instanceof MyEntity) {
//do something
}
...
Is there a way to do this? Thanks in advance
Upvotes: 1
Views: 322
Reputation: 3833
So this is how I solved it:
Added the request stack to the service:
<service id="myservice>
<argument type="service" id="request_stack"/>
</service>
Then got the controller like this:
// src/AppBundle/EventListener/AcmeListener.php
namespace AppBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class AcmeListener
{
/** @var Request */
protected $request;
/**
* AcmeListener constructor.
*
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->request = $requestStack->getCurrentRequest();
}
/**
* @param LifecycleEventArgs $args
*/
public function postLoad(LifecycleEventArgs $args)
{
$controller = $this->request->attributes->get('_controller');
if (strpos($controller, 'DontTriggerController::indexAction') !== false) {
// Do nothing
return;
}
// Do somethings
}
}
hope this helps someone
Upvotes: 1