How To Save Data Into A Database Table.

Hi All,

I have two tables, table_a and tabl_b.

after saving data in table_a, i want to save some columns of table_a those are currently saved into a database table of table_b.

any help plz…


What have you tried so far?

I tried this code for inserting values from model a to model b.

 protected function afterSave()


	$clientid = Client::model()->findByPk(Yii::app()->user->id);

	Packageassignment::model()->user_id = $clientid;

	Packageassignment::model()->package_id= $this-->package_id;

	Packageassignment::model()->package_start_date =$this->p_start_date;



   Packageassignment::model()->package_end_date   =$this->p_end_date;




And are you trying to create a new record in B or trying to update an existing one?

Note that at the moment, assuming the afterSave() method is in the class of $a, it will be called in an infinite loop as you’re saving $a within it’s own afterSave() method.

i have tried like below and its working for me::

protected function afterSave() { // "afterSave" of Post model



	$comment= Comment::model()->findByPk(7);


        $comment->description='copied from post';		



@Keith…plz see my updates…i just want to copy a record from current model to a database table.thanks

When do you need to make this copy? you may not need function afterSave() …

i need copy data after saving in database.i want only specific data to be copied into other database tables.



protected function afterSave() //Copy data from Client to packageassignment *** assuming your you're in the Client model 

								//and Clien has id,package_id,p_start_date


	$packageassignment= new Packageassignment();





	return parent::afterSave();


Sir No record is copy from client to packageassignment.plz advise.thanks

@fouss…sir no data is copying into 2nd table…plz advise…thanks

Do you still get this problem?

First you want insert the table_a value then after if table_a insert the sucess then after you want to insert the table_b. but make sure your varible are diffrent so it’s easy.

just try it…


$model = new table_a()




$tableb = new table_b()


//insert the all value.


i hopr you got the my point.