Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Doctrine life cycle event from within a Symfony2 controller

I am looking to remove a Doctrine Extensions life cycle event listener from within a controller.

I need to remove the listener for update events because I need to update all nodes in the tree at once. Something that is not supported by the library, but is possible by directly setting the correct left, right, level etc...

Is it possible to remove a life cycle even from within a controller? What is a possible solution for this situation.

I thought something like this might work, but it did not

$evm = $em->getEventManager();

$listener = new \Gedmo\Tree\TreeListener();
$evm->removeEventListener( array( 'postUpdate' ), $listener );
like image 602
Mike Avatar asked Oct 05 '12 03:10

Mike


1 Answers

yes it will work, but there are different events used:

$listenerInst = null;
$em; /* entity manager */
foreach ($em->getEventManager()->getListeners() as $event => $listeners) {
    foreach ($listeners as $hash => $listener) {
        if ($listener instanceof WantedListenerClass) {
            $listenerInst = $listener;
            break 2;
        }
    }
}
$listenerInst || die('Listener is not registered in the event manager');
// then you can remove events you like:
$evm = $em->getEventManager();
$evm->removeEventListener(array('onFlush'), $listenerInst);
like image 90
Gediminas Avatar answered Nov 02 '22 08:11

Gediminas