直接上代码:
$db = Yii::$app->db; $transaction = $db->beginTransaction(); //开启事务 try { // 更新member表 $db->createCommand()->update(‘member‘,[ ‘level‘ => $data[‘level‘] ],[‘id‘=>$data[‘user_id‘]])->execute(); // 更新storage表 $db->createCommand()->update(‘storage‘,[ ‘data‘ => $data[‘data‘], ],[‘member_id‘=>$data[‘user_id‘]])->execute(); $transaction->commit(); // 两条sql均执行成功,则提交 } catch (Exception $e) { $transaction->rollBack(); // 事务执行失败,则回滚 }
时间: 2024-10-09 14:02:34