Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I update only certain fields in a Yii framework?

Tags:

php

yii

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??

like image 665
I'm Geeker Avatar asked Nov 23 '12 10:11

I'm Geeker


2 Answers

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

like image 117
slaff.bg Avatar answered Nov 17 '22 22:11

slaff.bg


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.

like image 5
Nikos Tsirakis Avatar answered Nov 17 '22 23:11

Nikos Tsirakis