kalpit
(Panditkalpit)
September 12, 2013, 4:44am
1
i have 7 attributes in my table… and here i want to store only 4 attributes when i call $model->save().
i referred this link to do this thing.
as per above link i wrote my code like this…
if($model->save(array($model->var1,$model->var2,$model->var3,$model->var4)))
$this->redirect(...);
but it’s storing all 7 attributes…
Can anyone suggest me what i am doing wrong in this code??
Thanks
Ankit_Modi
(Ankit Modi)
September 12, 2013, 4:50am
2
kalpit:
i have 7 attributes in my table… and here i want to store only 4 attributes when i call $model->save().
i referred this link to do this thing.
as per above link i wrote my code like this…
if($model->save(array($model->var1,$model->var2,$model->var3,$model->var4)))
$this->redirect(…);
but it’s storing all 7 attributes…
Can anyone suggest me what i am doing wrong in this code??
Thanks
Hi if you want store the only 4 values you may like the first create the object
for ex.
$model = new CustomerForm();
$model->$model->var1 = $_POST['CustomerForm']['var1'];
$model->$model->var2 = $_POST['CustomerForm']['var2'];
$model->$model->var3 = $_POST['CustomerForm']['var3'];
$model->$model->var4 = $_POST['CustomerForm']['var4'];
$model->save(false);
kalpit
(Panditkalpit)
September 12, 2013, 5:09am
3
Hi if you want store the only 4 values you may like the first create the object
for ex.
$model = new CustomerForm();
$model->$model->var1 = $_POST['CustomerForm']['var1'];
$model->$model->var2 = $_POST['CustomerForm']['var2'];
$model->$model->var3 = $_POST['CustomerForm']['var3'];
$model->$model->var4 = $_POST['CustomerForm']['var4'];
$model->save(false);
if will give false then it won’t validate these 4 attributes… i also want to pass validation like not null
Ankit_Modi
(Ankit Modi)
September 12, 2013, 5:22am
4
ok just change remove the false
$model->save()
check this wiki
kalpit
(Panditkalpit)
September 12, 2013, 5:25am
5
if i will give (true) then it’s storing other 3 fields also… that’s way i am trying to give attributes list inside $model->save()…
Ankit_Modi
(Ankit Modi)
September 12, 2013, 5:33am
6
have you resovled issue ? and also check your Db filed if not resolved
kalpit
(Panditkalpit)
September 12, 2013, 5:38am
7
take a look of my method…
here i don’t want to update password field when i am updating gender,weight and all(basic details)…
for both i basic details and password change i have diff form in update… but when i click on basic details submit button i don’t want to update my password field and when i click on password change button it should not update basic details…
public function actionbasicDetails($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
$model->gender=$_POST['User']['gender'];
$model->height=$_POST['User']['height'];
$model->weight=$_POST['User']['weight'];
$model->dateofbirth=$_POST['User']['dateofbirth'];
if(isset($_POST['User']['Current_password']))
{
echo '<script> alert("hey"); </script>';
if($model->password==$model->hashPassword(($_POST['User']['Current_password'])))
{
$model->password=$_POST['User']['new_password'];
if($model->save())
$this->redirect(array('basicdetails','id'=>$model->id,'suc'=>'Info Succefully Updated'));
}
else
$this->redirect(array('basicdetails','id'=>$model->id,'err'=>'Password didn\'t Change...'));
}
if($model->save())
$this->redirect(array('basicdetails','id'=>$model->id,'suc'=>'Info Succefully Updated'));
else
$this->redirect(array('basicdetails','id'=>$model->id,'err'=>'Sorry ! Something went wrong.. Please try again'));
}
$this->render('basicdetails',array(
'model'=>$model,
));
}
without encryption it’s working fine but when i use md5() for encrypt password field it’s changing password …
i don’t know where i am doing wrong?
Ankit_Modi
(Ankit Modi)
September 12, 2013, 5:58am
8
kalpit:
take a look of my method…
here i don’t want to update password field when i am updating gender,weight and all(basic details)…
for both i basic details and password change i have diff form in update… but when i click on basic details submit button i don’t want to update my password field and when i click on password change button it should not update basic details…
public function actionbasicDetails($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
$model->gender=$_POST['User']['gender'];
$model->height=$_POST['User']['height'];
$model->weight=$_POST['User']['weight'];
$model->dateofbirth=$_POST['User']['dateofbirth'];
if(isset($_POST['User']['Current_password']))
{
echo '<script> alert("hey"); </script>';
if($model->password==$model->hashPassword(($_POST['User']['Current_password'])))
{
$model->password=$_POST['User']['new_password'];
if($model->save())
$this->redirect(array('basicdetails','id'=>$model->id,'suc'=>'Info Succefully Updated'));
}
else
$this->redirect(array('basicdetails','id'=>$model->id,'err'=>'Password didn\'t Change...'));
}
if($model->save())
$this->redirect(array('basicdetails','id'=>$model->id,'suc'=>'Info Succefully Updated'));
else
$this->redirect(array('basicdetails','id'=>$model->id,'err'=>'Sorry ! Something went wrong.. Please try again'));
}
$this->render('basicdetails',array(
'model'=>$model,
));
}
Hi i think you want to just remove the line and if change user change password click you want ot update the password filed else update the other filed so you can’t not write the if else statement
$model->attributes=$_POST['User'];
and
if(isset($_POST['User']['Current_password']))
{
}else{
if($model->save()){
$this->redirect(array('basicdetails','id'=>$model->id,'suc'=>'Info Succefully Updated'));
} else{
$this->redirect(array('basicdetails','id'=>$model->id,'err'=>'Sorry ! Something went wrong.. Please try again'));
}
}
kalpit
(Panditkalpit)
September 12, 2013, 6:01am
9
Without encrypting password field this whole code is working fine…
but wenn i add encryption for password field at that time it’s changing password field also…
Ankit_Modi
(Ankit Modi)
September 12, 2013, 6:05am
10
have you try it?
$model->password=md5($_POST['User']['new_password']);
kalpit
(Panditkalpit)
September 12, 2013, 6:38am
11
yes i tried with this also
Ankit_Modi
(Ankit Modi)
September 12, 2013, 6:46am
12
you want to just add the like
$modelForm->password_repeat = $data['ChangePasswordForm']['password_repeat'];
$model->password = md5($modelForm->password_repeat);
kalpit
(Panditkalpit)
September 12, 2013, 7:17am
14
now it’s working fine for me everything… but is it good practice to do?
public function actionbasicDetails($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
if(isset($_POST['User']['gender']))
{
$gender=$_POST['User']['gender'];
$height=$_POST['User']['height'];
$weight=$_POST['User']['weight'];
$dob=$_POST['User']['dateofbirth'];
}
if(isset($_POST['User']['Current_password']))
{
if($model->password==md5($_POST['User']['Current_password']) && $_POST['User']['new_password']==$_POST['User']['password_repeat'])
{
$pass=md5($_POST['User']['new_password']);
$q = "update user set password='$pass' where id=$id";
$QueryResult=Yii::app()->db->CreateCommand($q)->query();
if($QueryResult)
$this->redirect(array('basicdetails','id'=>$model->id,'suc'=>'Info Succefully Updated'));
}
else
$this->redirect(array('basicdetails','id'=>$model->id,'err'=>'Password didn\'t Change...'));
}
$q = "update user set gender='$gender',height=$height,weight=$weight,dateofbirth='$dob' where id=$id";
$QueryResult=Yii::app()->db->CreateCommand($q)->query();
if($q)
$this->redirect(array('basicdetails','id'=>$model->id,'suc'=>'Info Succefully Updated'));
else
$this->redirect(array('basicdetails','id'=>$model->id,'err'=>'Sorry ! Something went wrong.. Please try again'));
}
$this->render('basicdetails',array(
'model'=>$model,
));
}
Ankit_Modi
(Ankit Modi)
September 12, 2013, 7:43am
15
yes but your code so lengthy and why don’t you create the different change_password from?
kalpit
(Panditkalpit)
September 12, 2013, 8:36am
16
Yup going for that only… first just i was testing it and now i am going to make separate method only…
Thanks for always helping me bro…