transaction commit when its validation fail

Hi, i have issue in using transaction.




public function actionCreate()

    {

        $model = new PoAgen();

        $seq = Sequence::FindOne(['seq_id' => 'INV/AG', 'seq_name' => (int)date('ymd')]);

        if(is_null($seq))

        {

            $_seq = new Sequence();

            $_seq->seq_id = 'INV/AG';

            $_seq->seq_name = (int)date('ymd');

            $_seq->value = 0;

            $_seq->save();

            $model->trx_id = $_seq->seq_id . '/' . $_seq->seq_name . str_pad($_seq->value+1, 3, "0", STR_PAD_LEFT);

        }

        else {

            $seq->value += 1;

            $model->trx_id = $seq->seq_id . '/' . $seq->seq_name . str_pad($seq->value, 3, "0", STR_PAD_LEFT);

            $seq->update();

        }


		$model->pot_cong = 0;

		$model->pot_basah_kg = 0;

		$model->pot_tangkai = 0;

		$model->ppn = 0;

		$model->pot_pinjaman = 0;

		$model->pot_panjar = 0;

		$model->ongkos_angkut = 0;

		$model->pot_angkut = 0;

        $model->is_transfer = true;

        $model->buy_date = date('Y-m-d');


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

            $transaction = PoAgen::getDb()->beginTransaction();

            try

            {

                $model->created_by = Yii::$app->user->identity->id;

                $model->created_time = date('Y-m-d H:i:s');


                $saldo = Saldo::findOne(1); 

                $kas = Kas::findOne(1000); // pembelian agen pks

                $agen = Agen::findOne($model->agen_id);

                

                if(!$model->is_transfer)

                {

                    if($kas->code == 'D')

                    {

                        $saldo->balance -= $model->total_bayar;

                    }

                    else 

                    {

                        $saldo->balance += $model->total_bayar;

                    }

                    $saldo->update();

                }

                $model->save();


                // posting to kasbook

                $kasbook = new KasBook();

                $kasbook->kasbook_id = uniqid();

                $kasbook->kas_date = $model->buy_date;

                $kasbook->ref_trxid = $model->trx_id;

                $kasbook->kas_id = $kas->kas_id;

                $kasbook->code = $kas->code;

                $kasbook->total = $model->total_bayar;

                $kasbook->balance = $saldo->balance;

                $kasbook->received_by = '['. $model->agen_id . '] ' . $agen->agen_name;

                $kasbook->remark = 'Berat Bersih : ' . $model->r_bersih . ', Harga : ' . $model->price;

                $kasbook->vehicle_id = $model->vehicle_id; 

                $kasbook->is_transfer = $model->is_transfer;

                $kasbook->created_by = Yii::$app->user->identity->id;

                $kasbook->created_time = date('Y-m-d H:i:s');

                $kasbook->save();

                

                if($model->ppn != 0)

                {

                    $kb_1 = new KasBook();

                    $kb_1->kasbook_id = uniqid();

                    $kb_1->kas_date = $model->buy_date;

                    $kb_1->ref_trxid = $model->trx_id;

                    $kb_1->kas_id = 2000;

                    $kb_1->code = 'K';

                    $kb_1->total = $model->ppn;

                    $kb_1->balance = $saldo->balance;

                    $kb_1->remark = 'Ppn Pembelian Agen';

                    $kb_1->vehicle_id = $model->vehicle_id;

                    $kb_1->is_transfer = true;

                    $kb_1->created_by = Yii::$app->user->identity->id;

                    $kb_1->created_time = date('Y-m-d H:i:s');

                    $kb_1->save();

                }


                // kurang posting ke purchase order barang

                $transaction->commit();

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

            }

            catch(Exception $e) {                

                $transaction->rollBack();

                throw $e;

            }            

        } else {

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

                'model' => $model,

            ]);

        }

    }



the validation failed in the block




if($model->ppn != 0) { ... }



because master table $kas do not have primary id 2000 yet, and the transaction still commit.

please advice.

Thanks before

I got answer from stackoverflow.com and the answer is $model->save() does not throw any exceptions but return boolean.

So I need to catch all the save() method in one variable to validate whether it is valid or not.




$valid = $model->save();

$valid = $valid && $saldo->save();

$valid = $valid && $kb_1->save();


if($valid) {

	$transaction->commit();

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

}

else

{

	$transaction->rollBack();

	throw new Exception("Error Processing Request", 1);

}