• 5
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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

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

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

  • 7
Reply Report

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
Reply Report
    • if you follow the scenarios options then for the action that you run now you must set the scenario just after loading the $model. By removing the password field from the view the validation will not require this attribute. Please specify your error to have a better idea of your problem.
    • If you set the scenario inside your controller in the used action then only the three fields will be required to save the model. What you don't understand? Run your scripts and if there is an error give us the error message.
    • yes i didnt understand.Im new to yii if($id==Yii::app()->user->id){ $model=$this->loadModel($id); if(isset($_POST['JbJsJobResume'])) { $model->attributes=$_POST['JbJsJobResume']; if($model->save()) $this->redirect(array('view','id'=>$model->id)); } $this->render('update',array( 'model'=>$model, )); } this is in controller now.how to add scenario ??
$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();
  • 2
Reply Report
    • $model->name=$_POST['JbJsJobResume']['name']; whether is ['name'] is db field name or form field name ?
      • 2
    • $model->name here name is the table column name and $_POST['JbJsJobResume']['name']; is the form data . print_r($_POST) and find in which post variable the data is.

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

  • 1
Reply Report

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)
  • 1
Reply Report

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.

  • -1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions