2013-03-13 2 views
2

services.xml의 파일을 작동하지 않는 교리 리스너를 등록 :심포니 2

<?xml version="1.0" ?> 

    <container xmlns="http://symfony.com/schema/dic/services" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> 

     <services> 
      <service id="task.task_history_insertion" class="Acme\Bundle\EventListener\TaskHistoryInsertion"> 
       <argument type="service" id="service_container" /> 
       <tag name="doctrine.event_listener" event="postPersist" method="postPersist"/> 
      </service> 
     </services> 
    </container> 

TaskHistoryInsertion.php

postPersist가 지속 후에 호출되지 않는 이유에 어떤 아이디어
class TaskHistoryInsertion implements EventSubscriber 
{ 

    protected $container; 
public function __construct(ContainerInterface $container) 
{ 
    $this->container = $container; 
} 

public function getSubscribedEvents() 
{ 
    return array(
     Event::postPersist 
    ); 
} 

public function postPersist(LifecycleEventArgs $args) 
{ 
     //not being called 
     } 
} 

?

답변

2

이벤트 구독자와 이벤트 수신기를 혼합합니다!

나는 이벤트 리스너 갈 것입니다 :

당신이

use Doctrine\ORM\Event\LifecycleEventArgs; 

사용하십시오

implements EventSubscriber 

public function getSubscribedEvents() 
{ 
    return array(
     Event::postPersist 
    ); 
} 

를 제거하고 services.xml 파일을 얻을 수 있음 src/Acme/Bu에로드되었습니다. ndle/DependencyInjection/AcmeExtension.php.

캐시를 지우면 제대로 작동합니다.

공식 문서

은 당신이 당신의 서비스에 대한 권리 태그를 사용하십시오 http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html

+1

에 대해 훨씬 더 명확한 그림을 제공 할 수있는 링크가 있습니다 : 어떤 가입자는 또한 유효 청취자입니다 - https://github.com/doctrine/common/blob/2.3.0 참조 /lib/Doctrine/Common/EventManager.php#L126-L135 – Ocramius

3

에서 찾을 수 있습니다. 당신은 doctrine.event_subscriber를 사용해야합니다 : 당신의 EventListener를 구현하려는 경우

<service id="task.task_history_insertion" class="Acme\Bundle\EventListener\TaskHistoryInsertion"> 
    <argument type="service" id="service_container" /> 
    <tag name="doctrine.event_subscriber"/> 
</service> 
0

이 - 정확히 이벤트처럼 리스너 클래스의 메소드의 이름을합니다. 귀하의 예에서는 postPersist라는 공용 메서드가 있어야합니다. 그리고 리스너 클래스는 EventSubscriber를 구현해서는 안됩니다. 이 주제에 당신의 정보를 http://docs.doctrine-project.org/en/latest/reference/events.html

+0

'2.0.x' 문서를 참조하지 마십시오. 대신에'latest'를 사용하십시오 : docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html – Ocramius

+0

나는 당신의 답을 downvote하지 않았다 :) 나는 단지 당신이 그 사실을 깨닫게하고 싶었다. 항상 최신 문서를 참조해야합니다. 그게 많은 도움이됩니다! – Ocramius