$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()]);
}
转载于:https://www.cnblogs.com/pcx105/p/6904767.html
本文详细介绍了在Yii框架中如何使用事务处理进行数据删除操作,包括从主模型及其关联模型中删除数据的过程。通过示例代码展示了如何开始事务、执行删除操作、提交或回滚事务,以及异常处理。

被折叠的 条评论
为什么被折叠?



