我有这样的用户位于AppBundle\DoctrineListeners
身体的正是这种Symfony3.3,Doctrine2。 EventSubscriber,在更新不执行
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
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"})
为什么它不被执行?
主义'EventSubscribes'(http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html)和'EntityListeners'(https://开头的symfony。 com/doc/current/bundles/DoctrineBundle/entity-listeners.html)是不同的东西。在这里,与自动装配无关(您的订户没有依赖关系)。但是,如果您处于3.3版本中,则可以使用服务自动加载和自动配置来自动将此类注册为具有适当标记的服务。 –