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

How to update password field through CakePHP form

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

$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();
        }

试试这个:

 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'));  

            }

字符串值应该用引号括起来。

在模型中保存之前:

 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']);
}

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