Skip to content

Instantly share code, notes, and snippets.

@davidmintz
Last active March 1, 2017 22:03
Show Gist options
  • Save davidmintz/13e0d3ecdcd13bbcf50c1151e1595058 to your computer and use it in GitHub Desktop.
Save davidmintz/13e0d3ecdcd13bbcf50c1151e1595058 to your computer and use it in GitHub Desktop.
just a snippet for discussion purposes
<?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