简体   繁体   English

为什么Symfony2不为preUpdate触发我的Doctrine事件侦听器?

[英]Why isn't Symfony2 triggering my Doctrine event listener for `preUpdate`?

I am attempting to add a simple Blowfish password encoder to Symfony2. 我试图将一个简单的Blowfish密码编码器添加到Symfony2。

When a user model is updated with a new password then I need to trigger the password to be encoded before the model is persisted. 当用新密码更新用户模型时,我需要在持久化模型之前触发密码进行编码。 I want to use the preUpdate Doctrine event tag in my services.xml . 我想在我的services.xml使用preUpdate Doctrine事件标记。

Unfortunately it is not being triggered. 不幸的是它没有被触发。

Oddly I am also subscribing to the prePersist event and that is being triggered correctly. 奇怪的是,我还订阅了prePersist事件,并且该事件已被正确触发。

Is there some difference I am missing or some reason this does not work? 我缺少某些区别还是某些原因导致此问题不起作用?

services.xml services.xml

<service id="base.user.listener.persist" class="Base\UserBundle\Listener\PasswordEncoder">
    <tag name="doctrine.event_listener" event="preUpdate" />
    <tag name="doctrine.event_listener" event="prePersist" />
    <argument type="service" id="security.encoder.blowfish" />
</service>

PasswordEncoder.php (event subscriber) PasswordEncoder.php(事件订阅者)

/**
 * @param LifecycleEventArgs $args
 */
public function prePersist(LifecycleEventArgs $args) {
    $entity = $args->getEntity();
    $entity = $this->encodePassword($entity);
}

/**
 * @param LifecycleEventArgs $args
 */
public function preUpdate(LifecycleEventArgs $args) {
    die('preUpdate');
    $entity = $args->getEntity();
    $entity = $this->encodePassword($entity);
}

If you need to see more code just sing out. 如果您需要查看更多代码,只需大声疾呼。

OK so there is nothing wrong with the services.xml definition. 好的,因此services.xml定义没有问题。 It seems that you must duplicate the subscription information in the listening class ( PasswordEncoder.php ) with: 看来您必须使用以下方式在侦听类( PasswordEncoder.php )中复制订阅信息:

public function getSubscribedEvents() {
    return array(
        Events::prePersist,
        Events::preUpdate,
    );
}

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

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