我有一个表单来编辑存储在 mysql db 中的 UserProfile。其中包括以下自定义配置:
public function configure()
{
$this->widgetSchema['password']=new sfWidgetFormInputPassword();
$this->validatorSchema['password']->setOption('required', false); // you don't need to specify a new password if you are editing a user.
}
当用户尝试保存时,调用 executeUpdate 方法来提交更改。如果密码留空,密码字段设置为“”,但我希望它保留旧密码而不是覆盖它。
这样做的最佳方式(/最符合 symfony 精神)是什么?我的解决方案是覆盖模型上的 setter 方法(我已经为密码加密做过),并忽略空白值。
public function setPassword( $password )
{
if ($password=='') return false; // if password is blank don't save it.
return $this->_set('password', UserProfile ::encryptPassword( $password ));
}
它似乎像这样工作得很好,但是有更好的方法吗?
如果您想知道我无法在此项目中使用 sfDoctrineGuard,因为我正在处理旧数据库,并且无法更改架构。