2017-07-26 27 views
0

我有這樣的用戶位於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"}) 爲什麼它不被執行?

+0

主義'EventSubscribes'(http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html)和'EntityListeners'(https://開頭的symfony。 com/doc/current/bundles/DoctrineBundle/entity-listeners.html)是不同的東西。在這裏,與自動裝配無關(您的訂戶沒有依賴關係)。但是,如果您處於3.3版本中,則可以使用服務自動加載和自動配置來自動將此類註冊爲具有適當標記的服務。 –

回答

0

正如@YannEugonénoted in comment - EventSubscribes和學說EntityListeners是不同的東西。在Symfony的事件偵聽器都被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 } 
+0

我已經iserted服務代碼,但我仍然沒有看到'onFlush'函數被調用 – Viszman

+0

@Viszman哦,我忽略了。你有Symfony事件訂戶,Doctrine事件訂戶和Doctrine事件監聽器。我更新了代碼。 – jkucharovic

+0

當我更改service.yml中的名稱,然後我得到加載失敗的錯誤,所以類被加載。我得到這樣的更新前和postUpdate事件執行的,但我想蔡夫訪問這兩個屬性,或最小到老,我readed我能做的就是那麼事件'onFlush'是由我的類函數派遣不叫 – Viszman