事务操作的表引擎不能为MyISAM,MyISAM是不支持事务的,可以是innodb
Db::startTrans();
try {
//删除该动态的评论
Db::name('comment')->where(['dynamic_id'=>$dynamic_id])->delete();
//删除收藏或者点赞该动态的记录
Db::name('like_collection')->where(['dynamic_id'=>$dynamic_id])->delete();
//动态数量减1
Db::name('users')->where(['users_id'=>$userInfo['users_id']])->setDec('dynamic_num');
//最后删除动态
Db::name('dynamic')->where(['dynamic_id'=>$dynamic_id])->delete();
$img_arr = explode(',',$dynamic['img']);
foreach ($img_arr as $key => $value) {
if ($value && file_exists('.'.$value)) {
unlink('.'.$value);//销毁图片
}
}
// 提交事务
Db::commit();
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return returnData1(201,'删除失败');
}