我的此订阅者位于AppBundle\DoctrineListeners
正文中
namespace AppBundle\DoctrineListeners;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\OnFlushEventArgs;
use AppBundle\Entity\Bing\KeyWordReport;
use Doctrine\ORM\Events;
/**
* Listener used to on update fielt create history
*/
class KeywordSubscriber implements EventSubscriber
{
public function onFlush(OnFlushEventArgs $args)
{
throw new \Exception("Error Processing Request", 1); //for testing
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledEntityUpdates() as $updated) {
if ($updated instanceof KeyWordReport) {
//do some work
}
}
$uow->computeChangeSets();
}
public function getSubscribedEvents()
{
return [Events::onFlush => ['onFlush', 10], Events::preUpdate];
}
}
我使用symfony 3.3中引入的autoconfigure
service.yml
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
实体中的我在注释* @ORM\EntityListeners({"AppBundle\DoctrineListeners\KeywordSubscriber"})
为什么不执行?
答案 0 :(得分:0)
As @YannEugonénoted in comment - EventSubscribes和Doctrine EntityListeners是不同的东西。 Symfony中的EventListeners为registered manually:
namespace AppBundle\DoctrineListeners;
use Doctrine\ORM\Event\OnFlushEventArgs;
use AppBundle\Entity\Bing\KeyWordReport;
class KeywordListener
{
public function onFlush(OnFlushEventArgs $eventArgs)
{
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledEntityInsertions() as $entity) {
// … some action
}
foreach ($uow->getScheduledEntityUpdates() as $entity) {
// … some action
}
foreach ($uow->getScheduledEntityDeletions() as $entity) {
// … some action
}
foreach ($uow->getScheduledCollectionDeletions() as $col) {
// … some action
}
foreach ($uow->getScheduledCollectionUpdates() as $col) {
// … some action
}
}
}
#services.yml
services:
# …
AppBundle\DoctrineListeners\KeywordListener:
tags:
- { name: doctrine.event_listener, event: onFlush }