使用cake PHP更新个人详细信息时,DB上的密码自动更改

时间:2015-01-12 13:41:07

标签: php mysql cakephp

如果我更新任何这些个人详细信息,那么我点击保存它。它保存新数据并将我的登录密码也改为随机值

public function update_personal_details() {
     $this->layout = null ;
     $this->autoRender = false;

     $response = array('success' => true);
     if ($this->request->isPost()) {
        if($this->User->exists($this->Auth->user('id'))) {
          try {          
            $this->User->read(null, $this->Auth->user('id'));
            $this->User->set('first_name',$this->request->data["first_name"]);
            $this->User->set('last_name',$this->request->data["last_name"]);
            $this->User->set('mobile',$this->request->data["mobile"]);
            $this->User->set('city',$this->request->data["city"]);
            $this->User->save();
          } catch (exception $ex) {
             $response['success'] = false;
          }
        }
     }  

     return json_encode($response);
  }   

1 个答案:

答案 0 :(得分:1)

要确保cakePHP仅更新所需的cols,您可以在save命令上传递数据和字段列表:

Model::save(array $data = null, boolean $validate = true, array $fieldList = array())

只需查看Cookbook Save your Data

即可

对你而言,这应该有效:

$data = array();
$data['first_name'] = $this->request->data["first_name"]);
$data['last_name'] = $this->request->data["last_name"]);
$data['mobile'] = $this->request->data["mobile"]);
$data['city'] = $this->request->data["city"]);

$this->User->save(array('User' => $data), true, array('first_name', 'last_name', 'mobile', 'city'));

希望有所帮助