Yii Framework Forum: Save Same Data In Two Model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Save Same Data In Two Model Rate Topic: -----

#1 User is offline   Mithlesh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 18-October 12

Posted 05 January 2013 - 05:32 AM

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.
0

#2 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 385
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 05 January 2013 - 07:57 AM

View PostMithlesh, on 05 January 2013 - 05:32 AM, said:

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

Posted Image
0

#3 User is offline   Mithlesh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 18-October 12

Posted 05 January 2013 - 09:20 AM

View Postfouss, on 05 January 2013 - 07:57 AM, said:

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

Attached File(s)


0

#4 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 385
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 05 January 2013 - 09:49 AM

View PostMithlesh, on 05 January 2013 - 09:20 AM, said:

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

Posted Image
1

#5 User is offline   Mithlesh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 18-October 12

Posted 05 January 2013 - 11:10 AM

View Postfouss, on 05 January 2013 - 09:49 AM, said:

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.yiiframew...310#entry184310
0

#6 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 385
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 05 January 2013 - 12:03 PM

View PostMithlesh, on 05 January 2013 - 11:10 AM, said:

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.yiiframew...310#entry184310

Happy to know that helped you!
Posted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users