如何在不更改密码字段的情况下更新某些字段,例如名字或姓氏或电子邮件我的型号
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();
}
您也可以这样做。。。
public function beforeSave()
{
if(!$this->getIsNewRecord())
unset($this->password);
return parent::beforeSave();
}
希望它能帮助你。。
您可以使用Yii场景。例如,在您的模型中,您有下一个验证规则:
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博客上发现了这张收据。
UPD您可以使用静态方法对密码进行散列,并可以在每个位置调用它:
public static function hashPassword($password)
{
return CPasswordHelper::hashPassword($password);
}
为单个用户创建一个用户模型对象,然后通过为字段分配新值进行更新:
...
$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';
}
...
嗨,我所做的一切都在这里。。。。
我的控制器::
公共函数actionUpdate($id){$model=$this->loadModel($id,'User');
$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 } ?>
公共函数actionUpdate($id){
$model=$this->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,
));
}