I have a function that utilize db transaction like below,
public function actionInvpost($id) {
$request = Yii::$app->request;
$model = $this->findModel($id);
$connection = Yii::$app->db;
$transaction = $connection->beginTransaction();
try {
if ($model->branchTarget->creditLimit < $model->branchTarget->getCreditBalance() + $model->getTotalCharges()) {
throw new Exception(Yii::t('app', '{name} is over the credit limit, it cannot be invposted!', ['name' => $model->branchTarget->name]));
} else {
if (FormHeader::UNPOSTED == $model->status) {
$model->status = FormHeader::INVPOSTED;
if ($model->save()) {
// Insert FormStatus
$formStatus = new FormStatus();
$formStatus->headerFk = $model->getPrimaryKey();
$formStatus->status = FormStatus::FORM_INVPOSTED;
$formStatus->memo = '## Invpost by ' . Yii::$app->user->identity->username . ' ##';
if (!$formStatus->save()) {
throw new Exception(Yii::t('app', 'Error in saving branch transfer status!'));
}
$transaction->commit();
Yii::$app->getSession()->setFlash('success', $model->formNo . ' ' . Yii::t('app', 'is successfully invposted.'));
if ($request->isAjax) {
/*
* Process for ajax request
*/
Yii::$app->response->format = Response::FORMAT_JSON;
return ['forceClose' => true, 'forceReload' => '#crud-datatable-pjax'];
} else {
/*
* Process for non-ajax request
*/
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
throw new Exception(Yii::t('app', 'Error in saving branch transfer!'));
}
} else {
throw new Exception(Yii::t('app', 'Error for multiple submission!'));
}
}
} catch (Exception $e) {
$transaction->rollBack();
if ($request->isAjax) {
/*
* Process for ajax request
*/
return [
'title' => Html::icon('glyphicon glyphicon-exclamation-sign white') . ' ' . Yii::t('app', 'Invpost branch transfer'),
'size' => 'normal',
'backgroundHeader' => 'btn-danger',
'content' => Html::tag('span', 'Exception error! ' . $e->getMessage(), ['class' => 'text-danger']),
'footer' => Html::button(Html::icon('glyphicon glyphicon-remove') . ' ' . Yii::t('app', 'Close'), ['class' => 'btn btn-default pull-left', 'data-dismiss' => 'modal'])
];
} else {
/*
* Process for non-ajax request
*/
Yii::$app->getSession()->setFlash('error', 'Exception error! ' . $e->getMessage());
return $this->redirect(['view', 'id' => $model->id]);
}
}
}
Above code should handle the possibility of double clicking of the button that call this function. However, I am still getting this double click problem.
... FORM_INVPOSTED 91 2021-07-03 12:49:35
... FORM_INVPOSTED 91 2021-07-03 12:49:45
Does the transaction is the problem?
Any suggestion?
Thank you in advance.