yii如何更新用户字段而不改变对密码的影响

时间:2013-12-03 19:12:24

标签: php yii

如何更新,某些字段,例如名字或姓氏或电子邮件,而不对密码字段进行任何更改 我的模特

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('username, salt,  password, firstname, lastname, email', 'required', 'on' ),
        array('superuser, status', 'numerical', 'integerOnly'=>true),
        array('username, password, salt', 'length', 'max'=>32),
        array('firstname, lastname,image', 'length', 'max'=>80),
        array('email', 'length', 'max'=>128),

        array('id, username, password, salt, firstname, lastname, email,  superuser, status', 'safe', 'on'=>'search'),
    );
}

在模型中我有

public function beforeValidate()
{


    $this->salt = '1294455567';
    return parent::beforeValidate();

}

5 个答案:

答案 0 :(得分:2)

你也可以这样做......

    public function beforeSave()
    {
            if(!$this->getIsNewRecord())
                 unset($this->password);
            return parent::beforeSave();
    }

希望它可以帮到你..

答案 1 :(得分:0)

您可以使用Yii scenarions。例如,在您的模型中,您有下一个验证规则:

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

当您需要创建新用户时,需要设置验证方案:

$model = new User('create');
//some code
$model->validate();

或者:

$model = new User();
$model->scenario = 'create';
//some code
$model->validate();

当您需要更新时,用户 - 使用update方案而不是create

$model = User::model()->findByPk($someId);
$model->scenarion = 'update';
//some code
$model->validate();

另外,我在俄罗斯Yii博客上发现了这个receipt

UPD 您可以使用静态方法来哈希密码,并且可以在每个地方调用它:

public static function hashPassword($password)
{
    return CPasswordHelper::hashPassword($password);
}

答案 2 :(得分:0)

为单个用户创建用户模型对象,然后通过为字段分配新值来进行更新:

...
$user = User::model()->find("`t`.`username` = :USER", array(':USER' => 'example.user'));
$user->firstname = 'New First Name';
$user->lastname = 'New Last Name';
$user->email = 'email@user.new';

# Now update User here
if($user->update()) {
    echo 'Updated';
}
...

答案 3 :(得分:0)

大家,我所做的就是在这里......

我的控制器::

public function actionUpdate($ id){         $模型= $这 - > loadModel($ ID,'用户&#39);

    $oldImage           = $model->image;
   // $old_password       = $model->password;
    $model->password    = '';


    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['User']))
    {

        $model->attributes=$_POST['User'];

        //If image not uploaded then set old image
        if(!$this->uploadImage($model)){
            $model->image    = $oldImage;
        }

        if($model->save()){

            Yii::app()->user->setFlash('success', "Data Updated successfully"); // Flash Message
            $this->redirect(array('admin','id'=>$model->user_id));
        }

    }

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

我的模特

    public function rules()
    {



            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array('first_name, last_name, password, email,repeat_password', 'required',"on"=>'create'),
                array('first_name, last_name, email', 'required',"on"=>'update'),
                array('first_name, last_name', 'length', 'max'=>100),
                array('password', 'length', 'max'=>30,'min'=>6),
                array('repeat_password', 'compare', 'compareAttribute' => 'password', 'message'=>'Confirm Password must be same as password'),
                array('email', 'length', 'max'=>50),
                array('email', 'unique'),
                //array('image', 'file', 'types'=>'jpg, gif, png'),
                array('image', 'safe'),
                // The following rule is used by search().
                // @todo Please remove those attributes that should not be searched.
                array('user_id, first_name, last_name, email', 'safe', 'on'=>'search'),
            );


    }
public function beforeSave(){


        if(!empty($this->password))
            $this->password = md5($this->password);
        else
            unset($this->password);


        return parent::beforeSave();

    }

我的观点::

 <?php
        // If user update profile or update data then don't show password
        if( ((@Yii::app()->request->getParam('act')!=="profile") && empty($_GET["id"])) || @$_GET["act"]=='changepwd' )
        {

     ?>
    <div class="row">
        <?php echo $form->labelEx($model,'password'); ?>
        <?php echo $form->passwordField($model,'password',array('maxlength'=>20,'minlength'=>6)); ?>
        <?php echo $form->error($model,'password'); ?>
    </div>

    <div class="row"><!-- Confirm password -->
        <?php echo $form->labelEx($model,'Confirm Password'); ?>
        <?php echo $form->passwordField($model,'repeat_password',array('maxlength'=>20,'minlength'=>6)); ?>
        <?php echo $form->error($model,'repeat_password'); ?>
    </div>
    <?php } ?>

答案 4 :(得分:-1)

public function actionUpdate($ id){
        $模型= $这 - &GT; loadModel($ ID);

    if(isset($_POST['User']))
    {
        $password = $model->password;  
        $model->attributes=$_POST['User'];
        if($password === $model->password) {
            $model->password = $password;    
        } else {
            $model->password = md5(SALT . $model->password);        
        }
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

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