Updating 2 Models In One Action

I want to update 2 models(contact,cand) in one action of a controller for third model(user) . the code is as follows

public function actionProfile($type)

{


	


		


	$model=new contact;		


	$userid=Yii::app()->user->id;		


	$curr_date=date('d/m/Y');		


	$timestamp=CDateTimeParser::parse($curr_date,'dd/MM/yyyy');


	


	if(isset($_POST['experience']))


	{


		$cand_id= Yii::app()->db->createCommand('select id from  cand where user_id=:uid')->bindValue('uid',$userid)->queryRow();


		$candmodel=cand::model()->findByPk((int)$cand_id);       			


		$candmodel->experience=$_POST['experience'];


		$candmodel->current_location=$_POST['location'];


		$candmodel->current_salary=$_POST['salary'];	


		 


		if($candmodel->save()){


			echo "CAND SAVED";


		} else{				


			echo "CAND MODEL NOT SAVED";


		}	


		


				


	}


	


	if(isset($_POST['contact']))


	{


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


		$model->deleted='N';


		$model->created=$timestamp;


		$model->updated=$timestamp;


		$model->createdby=$userid;


		$model->updatedby=$userid;			


		if($model->save()){


			echo "SAVED";


			exit;					


		}


		


	}	


	


	$this->render('profile_pinfo',array(


			'model'=>$model,


	));	





	


}

The contact model gets updated successfully but the cand model does not get update . the else block " echo "CAND MODEL NOT SAVED"; " is running always , can anyone help me out

Hi Basant gaur,

CDbCommand::queryRow() returns a row. You can not get the cand’s id just by casting the returned value to integer.

http://www.yiiframework.com/doc/api/1.1/CDbCommand#queryRow-detail

Probably you must write like this:




	$candmodel=cand::model()->findByPk($cand_id['id']);



P.S.

Please use the code marking in order to make your post much easier to read.

HI softark,

Thanks a lot , I will use code marking now , It feels really nice to have a great comunity around a product.

HI softark,

Now I am able to load the model. if I print the model it gives me the complete record along with the latest value of the properties I am updating . But still the model does not get updated. can please give me some idea.

Hi softark,

I got it solved , I was rendering model partially , some rules were getting violated, thats why the model was not getting updated , thanks once again