hi all.
in yii2.
we want to use transaction.
how exactly it works in controller.
public function actionCreate()
{
$model = new Post();
$transaction = $model->getDb()->beginTransaction();
try {
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$transaction->commit();
} else {
return $this->render('create', [
'model' => $model,
]);
}
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
}
above code, is right or wrong?
public function actionUpdate($id)
{
$model = $this->findModel($id);
$transaction = $model->getDb()->beginTransaction();
try {
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
}
above code, is right or wrong?
public function actionDelete($id)
{
$model = $this->findModel($id);
$transaction = $model->getDb()->beginTransaction();
try {
$model->delete();
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
return $this->redirect(['index']);
}
above code, is right or wrong?
thank you all for your help