How To Store Some Attributes Using $Model->Save()

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);



if will give false then it won’t validate these 4 attributes… i also want to pass validation like not null

ok just change remove the false


$model->save()

check this wiki

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()…

have you resovled issue ? and also check your Db filed if not resolved

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?

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'));

}


}

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…

have you try it?


$model->password=md5($_POST['User']['new_password']);



yes i tried with this also

you want to just add the like




$modelForm->password_repeat = $data['ChangePasswordForm']['password_repeat'];


            $model->password = md5($modelForm->password_repeat);

where?

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,

		));

	}

yes but your code so lengthy and why don’t you create the different change_password from?

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…