Hi all,
I used yii auth extension (http://www.yiiframework.com/extension/auth/) and tried to implement DB transaction (CDbTransaction). Below is my code and it is working fine when I develop it and test it using superadmin.
public function actionUnpost($id) {
if (Yii::app()->request->isPostRequest) {
$model = $this->loadModel($id);
$qtyCheck = array();
$transaction = Yii::app()->db->beginTransaction(); // Transaction begin
try {
// If unpost this will resulting in negative remaining quantity, we should cancel this unpost!!!
foreach ($model->formDetails as $detail) {
if (isset($qtyCheck[$detail->id])) {
$qtyCheck[$detail->id] -= ($detail->quantity * $detail->type);
} else {
$qtyCheck[$detail->id] = Journal::model()->getRemReceivingQty($detail->id, $detail->locationFk) - ($detail->quantity * $detail->type);
}
// remaining has normal balance positif, so check the remaining should not be less than 0
if (0 > $qtyCheck[$detail->id]) {
throw new CDbException('Stok barang ' . $detail->item->name . ' menjadi negatif, FB tidak bisa diunposting!');
}
// Delete journal entries
foreach ($detail->journalEntries as $entry) {
$entry->delete();
}
}
$model->postingStatus = FormHeader::UNPOSTED;
$model->save(false);
$transaction->commit();
Yii::app()->user->setFlash('success', "FB $model->headerNo telah berhasil diunposting.");
} catch (Exception $e) {
$transaction->rollBack();
Yii::app()->user->setFlash('error', $e->getMessage());
}
$this->redirect(array('view', 'id' => $model->id));
} else {
throw new CHttpException(400, 'Invalid request. Please do not repeat this request again!');
}
}
But when I created other users with limited permissions, this code failed to work. The transaction is not working, I could not understand why. I have to move the commit to above positions to make it work.
...
$transaction->commit();
$model->postingStatus = FormHeader::UNPOSTED;
$model->save(false);
Yii::app()->user->setFlash('success', "FB $model->headerNo telah berhasil diunposting.");
...
It does not make sense to me. With the bottom code, the purpose of transaction will not be satisfied.
I used mysql innodb for the tables and should not be any problem with pdo or whatsoever, since it is work fine when the user is superuser.
FYI, I also use audittrail extension (http://www.yiiframework.com/extension/audittrail2/).
Any help with this?