CakePHP更新2列

时间:2015-08-22 20:19:47

标签: php cakephp

我有一个' Users'表和我保存所有帐户相关的数据。它包含id, username, password, status, removed, last_login

默认情况下,创建帐户时status = 2removed = 0

我有一个拒绝申请的功能。当管理员拒绝申请时,status = 0removed = 1

现在,我的问题是,当我尝试更新这些字段时,我也注意到我的密码字段也会更新。

这是我的拒绝代码:

   public function deny(){

        $user_id = $this->request->params['pass'][0];

        if(!$user_id){
            $this->Session->setFlash('Invalid action!', 'alert_box', array('class' => 'alert-danger'), 'member'); 
            $this->redirect(array('controller' => 'admin', 'action' => 'member'));
        }else if($user_id){
            $this->User->id = $user_id;

            if($this->User->save(array('User' => array('status' => 0,'removed' => 1)))){
                $this->Session->setFlash('Successfully denied the application of the selected applicant!', 'alert_box', array('class' => 'alert-success'), 'member'); 
            }else{
                $this->Session->setFlash('Failed to deny the application of the selected applicant!', 'alert_box', array('class' => 'alert-danger'), 'member'); 
            }
            $this->redirect(array('controller' => 'admin', 'action'=>'member'));
        }
    }

我不确定为什么我的密码也会改变。但是有关更多信息,这是我的User :: beforeSave:

public function beforeSave($options = array()) {
        $this->data['User']['password'] = AuthComponent::password(
          $this->data['User']['password']
        );
        return true;
    }

2 个答案:

答案 0 :(得分:0)

你可以将callbacks选项添加到false吗?这样就不会执行beforeSave函数。

http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-save-array-data-null-boolean-validate-true-array-fieldlist-array

答案 1 :(得分:0)

您可以编辑beforeSave回调到此

public function beforeSave($options = array()) {
    if (isset($this->data['User']['password']) && $this->data['User']['password']) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }

    return true;
}

所以,只对密码进行哈希处理,并且只有在给定数组中存在密码