【发布时间】:2012-05-02 10:28:25
【问题描述】:
我正在尝试向 Symfony2 添加一个简单的 Blowfish 密码编码器。
当使用新密码更新用户模型时,我需要在模型被持久化之前触发密码进行编码。我想在我的services.xml 中使用preUpdate Doctrine 事件标签。
不幸的是它没有被触发。
奇怪的是,我还订阅了prePersist 事件,并且该事件被正确触发。
我是否遗漏了一些差异或某些原因这不起作用?
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(事件订阅者)
/**
* @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);
}
如果您需要查看更多代码,请大声说出来。
【问题讨论】:
-
@ilanco 第一个是的,但它不适用,因为我没有在模型/实体中使用注释(因为它无法访问容器或 Blowfish 编码器)。第二个看起来很有希望 - 感谢您将其链接。虽然这仍然不能解释事件永远不会触发并且
die()永远不会显示。无法更新实体是另一个问题。
标签: php symfony doctrine-orm