I am attempting to add a simple Blowfish password encoder to 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.
Unfortunately it is not being triggered.
Oddly I am also subscribing to the prePersist event and that is being triggered correctly.
Is there some difference I am missing or some reason this does not work?
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)
/**
* @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.xmldefinition. It seems that you must duplicate the subscription information in the listening class (PasswordEncoder.php) with: