Call to undefined method stdClass::save()

I have two tables: tbl_message and tbl_messageto.

When message is posted, the record at first saves in tbl_message [assume that the record’s id is 3 ] after the same record must be inserted into tbl_messageto.

I did but in the table ‘tbl_messageto’ only once record is posted. Is there any technique so that the record can be inserted twice??

I get an error Call to undefined method stdClass::save()

My code is :




    	$model = new Message;

		$modelMSGTo = new Messageto;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

			$transaction = $model->dbConnection->beginTransaction();

			

			try{

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

				// access member id

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

					

				// first step 

				if($model->save())

				{

					$modelMSGTo->messageId = $model->messageId;

					$modelMSGTo->memberId =$model->toAddress;

					$modelMSGTo->authorId =$model->authorId;

					$modelMSGTo->isNew =(int)1;

					$modelMSGTo->isUnread = (int)1;

					$modelMSGTo->messageStatusId = (int)1;

                	

           		// second step

					if($modelMSGTo->save())

					{

                    	/**

                   		*  now every field should be same except  memberId 

                      	*   now memberId 's value should be current authorId

                 		*/


						//$modelMSGTo->messageId = $model->messageId;

						$modelMSGTo->memberId =$model->authorId;

						//$modelMSGTo->authorId =$model->authorId;

						//$modelMSGTo->isNew =(int)1;

						//$modelMSGTo->isUnread = (int)1;

						//$modelMSGTo->messageStatusId = (int)1;

						

						$modelMSGTo->save();

						//if($modelMSGTo->save())

							$this->redirect(array('view','id'=>$model->messageId));

					}

					else

					{

						throw new CException($modelMSGTo->getErrors());

					}

				}

				else

				{

					throw new CException($model->getErrors());

				}

					

				$transaction->commit();

				Yii::app()->end();

			}

			catch(CException $er){

				$transaction->rollBack();

			}			

		}


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

			'model'=>$model,

		));



save() checks if the record is new to make an INSERT… and if it’s not (your case because it’s already saved) it make an UPDATE…

Before the second save() you need to tell Yii that this is a new record… you can do it with


$modelMSGTo->isNewRecord=true;

:rolleyes:Ya it worked! Thank you mdomba for your help.