Save Data To Multiple Table

Hello friends,

Using one view i want to add data in two table.

I have two table

1. customer

2. contactperson <-- this will be multiple based on customer_id of customer table

There is one customer and its multiple contact person so after add/update data of customer it will also add/update contactperson table with customer_id.

How to achive these in one action with ajax validation ?

I have attached snapshop.

Hi

for multiple instances of the same model read this

http://www.yiiframework.com/wiki/362/how-to-use-multiple-instances-of-the-same-model-in-the-same-form/

fit it in your needs,

If you have questions, please let us know :)

First create two objects of the models, one for each, as in


 $person = new Person;

$contactperson = new ContactPerson;



I cannot see your snapshot. But, using the auto-generated controller that gii makes for you, you can see this line:


// Uncomment the following line if AJAX validation is needed

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

Duplicate the above line, one for each model, that is($person,$contactperson). So you would have


$this->performAjaxValidation($person);

$this->performAjaxValidation($contactperson);

Then you could have something like this


if(isset($_POST['Person'], $_POST['ContactPerson']))

		{

			$person->attributes=$_POST['Person'];

			$contactperson->attributes=$_POST['ContactPerson'];			

			

			//assign pseudo contact_id to contactperson the purpose of validation

			$contactperson->contact_id = 3; //just a placeholder


//validate both models, assuming

$validate  = $person->validate() && $contactperson->validate();

if($validate)

{

       $person->save();

       $contactperson->contact_id = $person->contact_id;

      $contact_person->save();

}

			

}



If you have any question, feel free to ask

WELCOME to Yii

Hi

am new to yii and i dont know how to save data in second table after insert in first table…

I want to insert one form value and first table id in second table

My table structures is


My user table is

CREATE TABLE IF NOT EXISTS user (

id int(10) unsigned NOT NULL AUTO_INCREMENT,

username varchar(100) NOT NULL,

password varchar(100) NOT NULL,

fullname varchar(200) NOT NULL,

role varchar(200) NOT NULL,

created_by varchar(25) NOT NULL,

created_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

modified_by int(11) NOT NULL,

status char(10) NOT NULL,

clientid varchar(25) NOT NULL,

modified_date timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=106 ;

my client table is

CREATE TABLE IF NOT EXISTS user_client_map (

id int(10) unsigned NOT NULL AUTO_INCREMENT,

user_id int(11) NOT NULL,

client_id int(11) NOT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=28 ;

from THE FORM WE TAKE CLIENT ID AND SAVE DATA IN USER AFTER SAVING DATA IN USER TABLE WE SAVE THAT USER.ID AND CLIENTID FROM FORM SAVE TO user_client_map TABLE…

I DONT KNOW HOW TO MAP PLEASE GIVE SOLUTION FOR THIS ISSUE

THANX IN ADVANCE

Overrides the function afterSave() of the main model.


protected function afterSave()

{

    //create the new depended model with specified values and save it! for example

    $s = new DependedModel();

    $s->related_id = $this->id;

    $s->save();


    parent::afterSave();

}