如何通过CakePHP表单更新密码字段

时间:2015-07-02 11:09:55

标签: cakephp hash md5

我正在使用下面的代码。但它没有更新为哈希值。只需保存为输入值。请帮忙。

$this->request->data['User']['password'] = md5($this->request->data['User']['password']);
if ($this->User->updateAll(array('User.password' => $this->request->data['User']['password']),array('User.id' => $newsid))) {
$this->Session->setFlash("Password Changed");
$this->redirect(array('controller' => 'admins', 'action' => 'login'));  
          } else {
            $this->Session->setFlash("password not changed");
            $this->render();
        }

2 个答案:

答案 0 :(得分:2)

在模型中保存之前:

 public function beforeSave($option = array()) {
 if (isset($this->data[$this->alias]['password'])) {
 $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
  }
return true;
} 

控制器文件:

public function Change_password($id = null) {

$this->User->id = $id;
if ($this->User->save($this->request->data)) {
$this->User->saveField('password', $this->request->data['User']['password']);
}

如果在保存功能之前使用,如果密码字段不为空,则会自动保存为哈希格式。

答案 1 :(得分:1)

试试这个:

 if ($this->User->updateAll(array('User.password' => "'".$this->request->data['User']['password']."'"),array('User.id' => $newsid))) {   
                $this->Session->setFlash("Password Changed");
              $this->redirect(array('controller' => 'admins', 'action' => 'login'));  

            }

字符串值应该用倒置的逗号包装。