Submit Single Form Then Data Insert Into Two Tables

Hi,

I am new to yii framework. i want to do when i submit a form then the data will be inserted into multiple tables.

i tried many examples but not work for me.

i have 2 tables

    1. Organization (org_id, org_name,org_code, org_desccription)


    2. Contacts (contact_id, address1, address2, pincode)

views/Organization/_form.php

<div class="form">

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'royce-organization-form',


'enableAjaxValidation'=&gt;false,

));

//print_r($model);

//exit;

?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'org_code'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'org_code',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'org_code'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'org_name'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'org_name',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'org_name'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'org_description'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'org_description',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'org_description'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;contacts,'address1'); ?&gt;


	&lt;?php echo &#036;form-&gt;textArea(&#036;contacts,'address1',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;contacts,'address1'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;contacts,'address2'); ?&gt;


	&lt;?php echo &#036;form-&gt;textArea(&#036;contacts,'address2',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;contacts,'address2'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;contacts,'pincode'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;contacts,'pincode',array('size'=&gt;17,'maxlength'=&gt;17)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;contacts,'pincode'); ?&gt;


&lt;/div&gt;

views/Organization/create.php

<?php

/*$this->breadcrumbs=array(

'Organizations'=&gt;array('index'),


'Create',

);

$this->menu=array(

array('label'=&gt;'List Organization', 'url'=&gt;array('index')),


array('label'=&gt;'Manage Organization', 'url'=&gt;array('admin')),

);*/

?>

<? require_once"side-bar.php";?>

<h1>Create Organization</h1>

<DIV CLASS="mynavigation" ID="mynavigation">

<?php

$this->beginWidget(‘zii.widgets.CPortlet’, array(

		'title'=&gt;'Operations',


	));

$this->widget(‘zii.widgets.CMenu’,array(

		'items'=&gt;array(


			array('label'=&gt;'List Organization', 'url'=&gt;array('index')),


            array('label'=&gt;'Manage Organization', 'url'=&gt;array('admin')),


			)				


	));


	&#036;this-&gt;endWidget(); ?&gt;

</DIV>

<?php echo $this->renderPartial(’_form’, array(‘model’=>$model,‘contacts’=>$contacts)); ?>

<? require_once"footer.php";?>


Organization Controller

public function actionCreate()

{


	&#036;model=new Organization;


	&#036;contacts = new Contacts();


	


	if(isset(&#036;_POST['Organization']))


			{


			


			&#036;model-&gt;attributes=&#036;_POST['org_code'];


			&#036;model-&gt;attributes=&#036;_POST['org_name'];


			&#036;model-&gt;attributes=&#036;_POST['org_description'];


			


			if(&#036;model-&gt;save())


		


		  if(isset(&#036;_POST['Contacts'])){


			&#036;contacts-&gt;attributes=&#036;_POST['address1'];


			&#036;contacts-&gt;attributes=&#036;_POST['address2'];


			&#036;contacts-&gt;attributes=&#036;_POST['pincode'];


			


			if(&#036;contacts-&gt;save())


				&#036;this-&gt;redirect(array('view','id'=&gt;&#036;contacts-&gt;contact_id));


			}


			


			


		}


	





	&#036;this-&gt;render('create',array(


		'model'=&gt;&#036;model,


		'contacts'=&gt;&#036;contacts,


	));


}

Organization model

public function relations()

{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		


		'contact' =&gt; array(self::BELONGS_TO, 'Contacts', 'contact_id'),


		'address1' =&gt; array(self::BELONGS_TO, 'Contacts', 'address1'),


		'address2' =&gt; array(self::BELONGS_TO, 'Contacts', 'address2'),


		'pincode' =&gt; array(self::BELONGS_TO, 'Contacts', 'pincode'),			


		


	);


}

someone posted solution for this problem inthis url and i follow this steps http://www.yiiframework.com/forum/index.php/topic/28441-yii-one-form-and-multiple-models/, but not work for me…Please help me!

Hi Nanda,

Try this code in your controller section

Organization Controller

public function actionCreate()

{

$model=new Organization;

$contacts = new Contacts();

if(isset($_POST))

{

if($_POST[‘Organization’]) {

&#036;model-&gt;attributes=&#036;_POST['Organization'];


&#036;model-&gt;save();

}

if(isset($_POST[‘Contacts’])){

$contacts->attributes=$_POST[‘Contacts’];

$contacts->save();

}

//Redirect to your target page

$this->redirect(‘redirection path’);

}

$this->render(‘create’,array(

‘model’=>$model,

‘contacts’=>$contacts,

));

}

[color="#006400"]/* moved from Bug Discussions */[/color]