Search code examples
phpsymfonydoctrine-ormcommandevent-listener

Symfony 2: Disable Doctrine event listener in ContainerAwareCommand


I am using several Doctrine listeners registered in configuration file for some automatic updates (created_on, updated_on timestamps etc.). Currently I have implemented additional functionality that requires stashing prepared values in the database for easier searching.

I am thinking about update Symfony command that would prepare these values instead of SQL update script (actually any sort of change or update in the way the value is crated would than require just running this single command). However this would also trigger the EventListeners mentioned earlier.

Is there a way how to disable particular EventLister for single Command?


Solution

  • something like this should do the trick :

    $searchedListener = null;
    $em = $this->getDoctrine()->getManager();
    foreach ($em->getEventManager()->getListeners() as $event => $listeners) {
        foreach ($listeners as $key => $listener) {
            if ($listener instanceof ListenerClassYouLookFor) {
                $searchedListener = $listener;
                break 2;
            }
        }
    }
    if ($searchedListener) {
        $evm = $em->getEventManager();
        $evm->removeEventListener(array('onFlush'), $searchedListener);
    }
    else { //listener not found
    
    }