Last active
March 1, 2017 22:03
-
-
Save davidmintz/13e0d3ecdcd13bbcf50c1151e1595058 to your computer and use it in GitHub Desktop.
just a snippet for discussion purposes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyAwesomeModule; | |
use Zend\Mvc\MvcEvent; | |
use Doctrine\ORM\Events; | |
class Module | |
{ | |
public function onBootstrap(MvcEvent $e) | |
{ | |
$this->attachEntityListeners($e); | |
// a little other stuff | |
} | |
public function attachEntityListeners(MvcEvent $e) { | |
$application = $e->getApplication(); | |
$sm = $application->getServiceManager(); | |
$entityManager = $sm->get('entity-manager'); | |
$listener = $sm->get('My\Entity\Listener\SomeEntityListener'); | |
// this thing listens to only events happening in the entity with the annotation | |
// @ORM\EntityListeners({"My\Entity\Listener\SomeEntityListener"}) | |
$entityManager->getConfiguration()->getEntityListenerResolver()->register($listener); | |
// this UpdateListener listens to the specified events in _all_ entities | |
$entityManager->getEventManager() | |
->addEventListener([Events::postUpdate,Events::postRemove,Events::postPersist], | |
$sm->get('My\Entity\Listener\UpdateListener')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment