我在用户模型中有以下代码:
class User extends AppModel {
public function beforeSave($options = array()) {
if (!empty($this->data['User']['pass'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data['User']['pass'] = $passwordHasher->hash($this->data['User']['pass']);
}
return false;
}
}
当我使用:
通过UsersController创建新记录时$this->User->create();
$this->User->save(array('pass'=>'test'));
记录保存在数据库中,密码不会被加密。这意味着beforeSave没有被执行。有人能告诉我这里我做错了什么吗?
答案 0 :(得分:0)
我很惊讶密码甚至记录首先保存,因为您在false
回调中返回beforeSave()
。
public function beforeSave($options = array()) {
if (!empty($this->data['User']['pass'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data['User']['pass'] = $passwordHasher->hash($this->data['User']['pass']);
}
return true;
}