$model->startTrans(); $model->rollback(); $model->commit();
public function transaction() {
$modelA = model('A');
$modelA->startTrans(); $result = $modelA->save($data1); if($result === false){
$modelA->rollBack(); $this->error('添加A信息失败,请重试');
}
$modelB = model('B');
$modelB->startTrans(); $result = $modelB->save($data2); if($result === false){
$modelB->rollBack(); $modelA->rollBack(); $this->error('添加B信息失败,请重试');
}
$modelC = model('C');
$modelC->startTrans(); $result = $modelC->save($data3); if($result === false){
$modelC->rollBack(); $modelB->rollBack(); $modelA->rollBack(); $this->error('添加C信息失败,请重试');
} $modelC->commit();
$modelB->commit();
$modelA->commit(); $this->success('添加成功', url('admin/index/add'));
}