$transaction = Content::getDb()->beginTransaction();
try {
Content::updateAll(['title'=>'cccccccc','updated_at'=>time()],['id'=>1]);
$test = new \app\service\TestController();
$res = $test->actionA();
$test->actionB();
$transaction->commit();
}catch (\Exception $e){
$transaction->rollBack();
throw $e;
}
public function actionA()
{
$transaction = \Yii::$app->db->beginTransaction();
try {
Category::updateAll(['name'=>'ddddd'],['id'=>1]);
$transaction->commit();
}catch (\Exception $e){
$transaction->rollBack();
throw new \Exception('', 0, $e);
}
}
public function actionB()
{
$transaction = \Yii::$app->db->beginTransaction();
try {
$model = new Category();
$model->load(['name'=>'bbcccc'],'');
$model->save();
throw new \Exception('aaa');
$transaction->commit();
}catch (\Exception $e){
$transaction->rollBack();
throw $e;
}
}
嵌套事务回滚,直接抛出异常即可