[SOLVED] Problem to insert field from one table to other table

i hav two tables, there are table krs and table nilai1.

when i save the model of table krs, i hope the id(PK) of table krs can also be inserted to id_krs (table nilai1).

The structure of tables :

table krs


id(PK) | attribute1 | attribute2 |


table nilai1


id(PK) | id_krs(FK) | attribute3 | attribute4 |


Here the modification of KrsController.php




public function actionCreate()

	{

            $model=new Krs;

            $model2=new Nilai1;

            

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

                {

                $transaction=$model->dbConnection->beginTransaction();

                

                try{

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

                    if($model->save()){

                        $model2->id_krs=$model->getPrimaryKey;

                        

                        if($model2->save())

                        //print_r($model->getErrors()); die();

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

                    }

                    else

                        {

                        throw new CException($model->getErrors());

                    }

                    $transaction->commit();

                    Yii::app()->end();

                }

                catch(CException $er){

                    $transaction->rollBack();

                }

            }

            

            $this->render('create',array(

                'model'=>$model,'model2'=>$model2,

            ));

}

but, the result not like i want, not saving anything to the tables…

Somebody can help me???

what is the output of


$model->getPrimaryKey

?

integer value, i’m make id as auto_increment

maybe something wrong with my code?

So if Nilai1 BELONGS_TO Krs you can, for example use EAdvancedARBehavior to simplify things a little bit!

Anyway, from what you’ve provided us, it all seems to be ok, so I would guess that $transaction->rollBack() is called. Check the input again and make sure that commit() is called!

cheers

i have solved my problem

that i was remove the $transaction command also the try and catch

then i was add some code thats for identify the all fields of table nilai1

and now working

Hi sir,

Can u please tell how u will solved this , i have the same problem

please please reply me sir