Saving A Diferent Record After The Save Method

Hi community,

I’m trying to save a record to a table B (I have defined Model B with Gii), in my method ActionCreate of ControllerTableA after the save method which save the current record in TableA.

When I did the code listed below, I had a record with null values in my tableB.

Of course the record saved in TableA was correct.

The TableA is parent of TableB and the fields of TableB are also in TableA.

TableB has some fields with values of TableA plus some other constant values.

Here is the code in ControllerTableA:

private $_tablaB;

public function actionCreate()

{


  $model=new TABLE_A;


  if(isset($_POST['tableA']))


     {


     $model->attributes=$_POST['tableA'];


     if($model->save()) {


         $this->_tableB = new tablaB; Model TableB


         $this->_tableB->attributes = array('id'=>NULL, item1=>$model->id_item1, 'id_tablaA'=>model->ID,);


         $this->_tablaB->save();  


         $this->redirect(array('view','id'=>$model->iD));


     }


     $this->render('create',array( 'model'=>$model, ));


}

What I’m doing wrong? Any help is appreciated

Hi sanat,

This will set only 3 fields (‘id’, ‘item1’ and ‘id_tableA’) in the tableB.

Probably you have to add the following line before it:




$this->_tableB->attributes = $_POST['tableA'];



You mean this?

Thank you Softark, it worked. I only read the $_POST after the save and it works perfectly

Thank you Fouss.

Thank you Community