Save ke beberapa tabel

Mohon bantuannya!

Saya mempunyai 2 tabel: ReceivedGoodsDetail dan StockInventory.

Ketika model ReceivedGoodsDetail melakukan actionCreate maka di tabel StockInventory otomatis juga ikut bertambah.

Saya menggunakan store prcoedure.

Ini yang sudah saya buat:

public function actionCreate($id) {

    $model = new ReceivedGoodsDetail();





    $connection = \Yii::$app->db;


    $transaction = $connection->beginTransaction();





    $model->ID_Received_Goods = $id;


    if ($model->load(Yii::$app->request->post()) && $model->validate()) {


        $connection = Yii::$app->db;


        $command = $connection->createCommand('{callusp_T_Received_Goods_Detail#InsertData


        (:ID_Received_Goods,:ID_Item, :Qty, :User)}');





        $ID_Received_Goods = $model->ID_Received_Goods;


     


        $ID_Item = $model->ID_Item;


        $Qty = $model->Qty;


        $User = Yii::$app->user->identity->username;





        $command->bindParam(":ID_Received_Goods", $ID_Received_Goods, PDO::PARAM_STR);


        $command->bindParam(":ID_Item", $ID_Item, PDO::PARAM_STR);


        $command->bindParam(":Qty", $Qty, PDO::PARAM_INT);


        $command->bindParam(":User", $User, PDO::PARAM_STR);





        if ($command->execute() == 0) {


            $transaction->commit();


        } else {


            $transaction->rollBack();


            foreach ($model->getErrors() as $key => $message) {


                Yii::$app->session->setFlash('error', $message);


            }


        }





        return $this->redirect(['receivedgoodsheader/view', 'id' => $model->ID_Received_Goods]);


    } else {


        return $this->render('create', [


                    'model' => $model,


        ]);


    }


}

Tapi saya bingung jika memakai dua model seperti kasus saya di atas

klo pengen menghindari store procedure bisa menggunakan event pada yii seperti afterSave, beforeSave, beforeDelete, afterDelete dll.

contoh afterSave(letakkan pada model yg bersangkutan):




class ReceivedGoodsDetail extends \yii\db\ActiveRecord

{

...

    public function afterSave($insert, $changedAttributes) {

        // jika record baru $insert bernilai true, $changedAttributes berisi attribute yg berubah

        if($insert) {

            // letakkan kode disini

        }

        parent::afterSave($insert, $changedAttributes);

    }

...