Save Same Data In Two Model

Hi,

I am having two table comment1(id,content,create_time) and comment2(id,content,create_time)

I click on create comment1 and after writing someting in comment1 content box and clicking on create the same data should also get saved in comment2 content table.

Can anyone please suggest me.

Try this…it should work




	public function actionCreateComment1() //for the first button

	{

		$model=new Comment1;


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

		{

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

			$model->save();

		}


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

			'model'=>$model,

		));

	}


	public function actionCreateComment2() //for the second  button

	{

		$model=new Comment2;


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

		{

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

			$model->save();

		}


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

			'model'=>$model,

		));

	}



Hi fouss,

Thanks for your prompt reply…

I dont want 2 buttons, i want only one button, on submitting the form the content should get saved in both the tables/models.

Find the attachment.

Thanks

That’s so easy!

Assumming you get Comment and Comment2 models with the same table sheme




	public function actionCreate()

	{

		$model=new Comment1;

		$model2=new Comment2;


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

		{

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

			$model2->attributes=$_POST['Comment1'];

			if($model->save()&&$model2->save())

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

		}


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

			'model'=>$model,

		));

	}



Hi fouss,

Thanks you are champ, it worked well as expected.

Thanks again.

Can you please also help me in the below link if you can, I am following the below link and waiting for someone to update. I am also creating similar kind of thing.

http://www.yiiframework.com/forum/index.php/topic/37804-assigning-data-to-user/page__p__184310#entry184310

Happy to know that helped you!