yii2 事务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $transaction = Yii:: $app ->db->beginTransaction(); try { //删除$model中的数据 $res = $model ->deleteAll( $cond ); if (! $res ) throw new \Exception( '操作失败!' ); //删除$model对应的$relation中的数据 $rt = $relation ->deleteAll([ 'polymeric_id' => $cond [ 'id' ]]); if (! $rt ) throw new \Exception( '操作失败!' ); //以上执行都成功,则对数据库进行实际执行 $transaction ->commit(); return Helper::arrayReturn([ 'status' =>true]); } catch (\Exception $e ){ //如果抛出错误则进入catch,先callback,然后捕获错误,返回错误 $transaction ->rollBack(); return Helper::arrayReturn([ 'status' =>false, 'msg' => $e ->getMessage()]); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步