How can I update only certain fields in a Yii fram

2019-06-16 07:08发布

enter image description here

I dont want to update the password fields.how to use this.Im using md5 encode for password.So i dont want to update the password field in yii framework.any help appreciated??

标签: php yii
6条回答
成全新的幸福
2楼-- · 2019-06-16 07:46

I am not sure why this is a problem, and some code could help us to understand why. If you do not wish to capture / update the password, then why is the password field in the form?

If you remove the password field from the view, the value of the password field will not be posted back to controller and then it will not be updated.

There is a possibility that the above method does not work and this could be that in your User model, you are encrypting the password in the afterValidate method?:

protected function afterValidate()
{
    parent::afterValidate();
    $this->password = $this->encrypt($this->password);
}

public function encrypt($value)
{
    return md5($value);
}

In this scenario, if you remove the password field from the view, and just update the name, username or email, then the md5 hash of the password will be re-hashed automatically and you will lose the real password.

One method to get around this is to md5 the password in the afterValidate method (create or update) however if the user wishes to change profile details, in the same form, ask the user to verify their password again.

  1. FORM: user changes name and verifies password
  2. Form posted
  3. Controller calls authenticate method.
  4. If authenticate true, overwrite the entry in user table (including verified pw)
查看更多
走好不送
3楼-- · 2019-06-16 07:51

1st option Just unset password field before setting it:

function update(){
    $model=$this->loadModel($id);
    unset($_POST['JbJsJobResume']['password']);
    $model->attributes=$_POST['JbJsJobResume'];
    $model->save();  
}

2nd option: Use temp variable:

function update(){
    $model=$this->loadModel($id);
    $temPassword = $model->passwrod;
    $model->attributes=$_POST['JbJsJobResume'];
    $model->passwrod = $temPassword;
    $model->save();  
}

3rd option: use scenarios

查看更多
ら.Afraid
4楼-- · 2019-06-16 07:57

In your model you must do something like this:

public function rules() {
    return array(
        array('name, username, email, password', 'required', 'on' => 'create'),
        array('name, username, email', 'required', 'on' => 'update'),
    );
}

Lets say that the scenario that you run now is the update. So I don't require the password there. I require it only in the create scenario that you may have. So in the view file that you have you remove the password field and inside the action that you have you include this:

$model->setScenario('update');

so it will not require the password and it will remain the same.

For the password change you can create a new action (ex. actionPassChange) where you will require to type twice the new password.

查看更多
劳资没心,怎么记你
5楼-- · 2019-06-16 07:59
$model->attributes=$_POST['JbJsJobResume'];

instead of assign all attributes just assign those only you want to save, as

$model->name=$_POST['JbJsJobResume']['name'];
$model->save();
查看更多
Juvenile、少年°
6楼-- · 2019-06-16 08:05

I think a better approach would be to not use the scenario in this case. The next code in the rules just says to the scenario: the next fields are required. But not: skip the other else.

array('name, username, email', 'required', 'on' => 'update'),

For example, if we limit the length of the password up to 32 characters, but in a database is stored in a format sha1 (length 40), then we have a problem because the validator will block the database query.This is because when you make updating, the "validatе" method checks all class properties (regards database table mapping), not just the new ones delivered by post.

Could use the method "saveAttributes", but then I noticed another problem. If the column "email" is unique in the database and in case edited email duplicate one of the existing, then the Yii message system defined in the rules can not notify and throws error code regards database query.

The easiest approach I think is: don't set scenario in this case. Just send as an argument the properties you want. This will keep the all CRUD features created by GII.

In your code it looks like this: (in model)

public function rules() {
    return array(
        array('name, username, email, password', 'required'),
    );
}

(in controller)

if($id==Yii::app()->user->id){ 
    $model=$this->loadModel($id); 
    if(isset($_POST['JbJsJobResume'])) { 
        $model->attributes=$_POST['JbJsJobResume']; 
        if($model->save(true, array('name', 'username', 'email'))) 
            $this->redirect(array('view','id'=>$model->id)); 
    } 

    $this->render('update',array( 'model'=>$model, )); 
}

I noticed that you do not use RBAC. It is very convenient and flexible - try it.

http://www.yiiframework.com/doc/guide/1.1/en/topics.auth#role-based-access-control

查看更多
Emotional °昔
7楼-- · 2019-06-16 08:09

I think @Gravy's answer is right,Thanks Gravy and Nikos Tsirakis. I have fixed nearly same issue as @faizphp. I add scenario for User model as Nikos Tsirakis said, but got same issue also. Then I found I encrypt password in User.afterValidate, so when update the User model everytime, the program encrypt the password in database again to wrong password. So i changed my function from

protected function afterValidate()
{
        parent::afterValidate();
        if (!$this->hasErrors())
            $this->password = $this->hashPassword($this->password);
}
</code>


to

protected function afterValidate()
{
        parent::afterValidate();
        if (!$this->hasErrors() && $this->scenario==="create")
            $this->password = $this->hashPassword($this->password);
}

. It seems work.

查看更多
登录 后发表回答