2016-07-05 60 views
-2

嗨我想創建在我的包中做什麼的日誌。所以我正在使用Symfony2的OnFlush事件。併爲此創建了一個Listener。但它沒有被觸發。OnFlush無法正常工作Symfony2 ODM

//我的聽衆

namespace Edu\AccountBundle\EventListener; 
use Doctrine\ODM\MongoDB\DocumentManager; 
use Symfony\Component\EventDispatcher\EventDispatcherInterface; 
use Symfony\Bundle\FrameworkBundle\Routing\Router; 
use Edu\AccountBundle\CommonFunctions\CommonFunctions; 
use Edu\AccountBundle\Constants\allConstants; 
use Symfony\Component\HttpFoundation\Session\Session; 
use Doctrine\Common\EventSubscriber; 
use Doctrine\ODM\Event\OnFlushEventArgs; 

class OnFlushListener 
{ 
    private $session; 
    private $router; 
    private $commonFunctions; 
    private $constants; 

    public function __construct(Session $session, Router $router, DocumentManager $dm) 
    { 
     $this->session = $session; 
     $this->router = $router; 
     $this->dm = $dm; 
     $this->commonFunctions = new CommonFunctions(); 
     $this->constants  = new allConstants(); 
    } 

    public function onFlush(OnFlushEventArgs $event) 
    { 
     echo "come here after Saving data";die; 
    } 
} 

//config.xml

<service id="edu.account.event.listener" class="Edu\AccountBundle\EventListener\OnFlushListener"> 
    <argument type="service" id="session"/> 
    <argument type="service" id="router"/> 
    <argument type="service" id="doctrine_mongodb.odm.document_manager"/> 
    <tag name="doctrine.event_listener" event = "onFlush" method="onFlush"/> 
</service> 

當我試圖保存任何文件,它不會被調用。 「onFlush」方法。請指導我在這裏失蹤。

感謝提前

+0

請嘗試'退出(轉儲(「保存數據後來到這裏)))'。 – Alsatian

+0

仍然無法正常工作。它似乎控制不去那裏 –

+0

你的config.xml文件在哪裏?你清除了緩存嗎? – Alsatian

回答

0

您的服務標籤的名字是錯誤的,它應該是doctrine_mongodb.odm.event_listener。有關更多詳細信息,請參閱docs

+0

讓我試試這一個.. –

+0

給予這個錯誤現在: ServiceCircularReferenceException:服務「doctrine_mongodb.odm.default_document_manager」檢測到循環引用,路徑爲:「doctrine_mongodb.odm.default_document_manager - > doctrine_mongodb.odm.default_connection - > doctrine_mongodb.odm.event_manager - > edu.account.event.listener「。 –

+0

不要將'DocumentManager'注入到服務中,您可以從傳遞給您的方法的'Event'中獲得一個。 – malarzm