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

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

最佳答案

为了确保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'));


希望能有所帮助

关于php - 在使用Cake PHP更新个人详细信息时,数据库上的密码会自动更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27903340/

10-09 08:12
查看更多