我使用Yii2活动记录事务,但验证在代码中间失败,它仍然提交事务。 请指教。
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 = Yii::$app->db->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,
]);
}
}
验证在if($model->ppn !=0) { ... }
失败
因为主表中没有任何主键,但事务仍然执行提交并将模型插入到数据库中。
请指教。谢谢。
答案 0 :(得分:2)
save()
方法不会抛出异常但会返回布尔值,因此您需要显式抛出它。替换每个
...->save();
在try-catch中使用
if (! ...->save()) {
throw new \yii\db\Exception('Error while saving ... model!');
// or use general \Exception()
}
所以现在它可以被捕获。
答案 1 :(得分:0)
您可以通过以下方式执行此操作:
$flag=true;
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');
if(!$kb_1->save())
{
$flag=false;
}
}
if($flag)
$transaction->commit();
else
$transaction->rollback();
$flag=true;
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');
if(!$kb_1->save())
{
$flag=false;
}
}
if($flag)
$transaction->commit();
else
$transaction->rollback();