INSERTING DATA IN TWO TABLES AT A SINGLE TIME

HELLO, i am new to yii framework,

             what should i do to insert data in two table at single create.

example: i ve two tables table1(id1,name,type,action,address,status)

                     table2(id2,id1,name,action)

when i inserting data in table1 it should insert in table2 too, how should i do it.

You perform two sql insert statements in your controller function, one on each table. (That does not answer your question, but why would you want to use a single insert?)

Or you create an updatable view for the tables in your DBMS and perform an insert on that view.

Or you create a trigger on the first table in your DBMS and perform an insert on the first table.

U could use afterSave in table1 class. Please check:

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#afterSave-detail

1 Like

If ACID is your concern, use transactions.

here help yourself

http://www.yiiframework.com/wiki/559/tabular-input-validating-and-saving-related-models/

Lets say:

model of table1: Tab1

model of table2: Tab2

In Tab1:


public function afterSave(){

		parent::afterSave();

		

			$tab2 = new Tab2();

			$tab2->id1=$this->id1;

                        $tab2->name=$this->name;

                        $tab2->action=$this->action;

			$tab2->save();


		}

I supposed that name and action in table2 will be the same as in table1…

If not, then you have to set different values in afterSave method.