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…

Thanks

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;


	$clientid->Save(false);

   

}



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

	parent::afterSave();

				

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

	$comment->content=$this->name;

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

	$comment->save(false);

}

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

thanks

ok!




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();

	$packageassignment->user_id=$this->id;

	$packageassignment->package_id=$this->package_id;

	$packageassignment->package_start_date=$this->p_start_date;

	$packageassignment->save();

	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…




1)table_a


$model = new table_a()

$model->setattibutes();

$model->save(false);




2)table_b


$tableb = new table_b()

$tableb->table_a_id=>$model->id;

//insert the all value.

$tableb->save(false);



i hopr you got the my point.