CakePHP 2.4.5 - 之前没有调用beforeSave()

时间:2014-03-11 09:50:28

标签: php cakephp authentication model

我在用户模型中有以下代码:

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没有被执行。有人能告诉我这里我做错了什么吗?

1 个答案:

答案 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;
}

Reference