简体   繁体   English

Symfony EventDispatcher订户无法正常工作

[英]Symfony EventDispatcher subscriber dont work

The following code 以下代码

use Application\Events\TransactionCreatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;

class Transaction implements EventSubscriberInterface
{
    protected $date;
    protected $name;
    protected $address;
    protected $phone;
    protected $price_with_vat;
    protected $transaction_type;
    protected $receipt;
    protected $currency;


    protected function __construct($date, $name, $address, $phone, $price_with_vat, $transaction_type, $receipt, $currency)
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addSubscriber($this);
        $dispatcher->dispatch(TransactionCreatedEvent::NAME, new TransactionCreatedEvent($date, $name, $address, $phone, $price_with_vat, $transaction_type, $receipt, $currency));
    }

    public static function CreateNewTransaction($date, $name, $address, $phone, $price_with_vat, $transaction_type, $receipt, $currency){
        return new Transaction($date, $name, $address, $phone, $price_with_vat, $transaction_type, $receipt, $currency);
    }

    private function onCreateNewTransaction($Event){
        $this->date = $Event->date;
        $this->name = $Event->name;
        $this->address = $Event->address;
        $this->phone = $Event->phone;
        $this->price_with_vat = $Event->price_with_vat;
        $this->transaction_type = $Event->transaction_type;
        $this->receipt = $Event->receipt;
        $this->currency = $Event->currency;
    }

    public static function getSubscribedEvents()
    {
        return array(TransactionCreatedEvent::NAME => 'onCreateNewTransaction');
    }
}

it suppose to dispatch a TransactionCreated event and get caught by the class itself and the onCreatedNewTransaction function get invoke in order to set the properties of the class. 它假设调度一个TransactionCreated事件并被类本身捕获,并且onCreatedNewTransaction函数被get调用以设置类的属性。

The Transaction class instantiated like Transaction类实例化如

$Transaction = Transaction::CreateNewTransaction('6/6/2016', 'John'....);

but when i debug the project the $Transaction object have null values. 但是当我调试项目时, $Transaction对象具有null值。 I set a breakpoint at onCreateNewTransaction method and I found that thus function does not get invoked. 我在onCreateNewTransaction方法上设置了一个breakpoint ,但发现该函数不会被调用。

UPDATED 更新

Problem solved 问题解决了

`onCreateNewTransaction' should be public instead of private `onCreateNewTransaction'应该是公开的,而不是私有的

Your method CreateNewTransaction is static, so no instances of Transaction is created and therefore __constructor is never called. 您的方法CreateNewTransaction是静态的,因此不会创建Transaction实例,因此永远不会调用__constructor

This is regarding why this code doesn't work. 这与为什么此代码不起作用有关。

But, besides, I must say it's a total misuse of Event system of Symfony. 但是,除此之外,我必须说这完全是滥用Symfony的Event系统。 Using framework (no EventDispatcher component), you must not create EventDispatcher yourself. 使用框架(没有EventDispatcher组件),您不能自己创建EventDispatcher。 It's beeing created by FrameworkBundle and you should only inject event_dispatcher service into whatever you need. 它是由FrameworkBundle创建的,只event_dispatcher服务注入所需的内容。

Otherwise, you can get lost very quickly with different scopes (each dispatcher has it's own subscribers and it's own events), and besides, it' sa waste of resources. 否则,您可能会因不同的作用域而很快迷失(每个调度程序都有它自己的订阅者和它自己的事件),而且浪费资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM