tp5事务回滚操作多表
array(5) {
["title"] => string(9) "水电费"
["art_desc"] => string(0) ""
["imageurl"] => string(0) ""
["views"] => string(0) ""
["category_id"] => array(2) {
[0] => string(1) "3"
[1] => string(1) "5"
}
2.后台事务操作插入多表(文章表,文章和分类关联表)
public function RelaCateSave($data){ // 多表 开启事务操作
Db::startTrans();
try{
$res = model("article") -> save($data);
$art_id = model("article") -> getLastInsID(); //获得刚插入的id
// 获取所属分类(数组集合)
$category_id_arr = $data["category_id"];
foreach ($category_id_arr as $key => $value) {
$item = [
"art_id" => $art_id,
"category_id" => $value,
];
$result = model("ArtRelaCate") -> insert($item);
}
// 提交事务
Db::commit();
}catch(Exception $e){
// 回滚事务
Db::rollback();
dump($e->getMessage());
return $this->error("提交失败");
}
return $this->success("提交成功","article/index");
}
一张积分表,用户名 应返回积分balance(商品金额*10) 返还周期period(单位月) 每月1号应返回积分数average(sum/period) 剩余应返回积分balance(--average) 上次返还积分的时间 状态(0已返还清,1未返还清)
积分表里减积分,用户表里加积分,
开启事务
先算出time超过5天的时间$time = date("Y-m-d H:i:s", strtotime("-5 days"));
"select user from tablename where status=1 and time<"{$time}" limit 100";
为用户添加积分
"update tablename set balance=balance+100";
修改积分表余额与状态
"update tablename set balance=balance-100 where status=1 and time<"{$time}"";
查询积分表status=1 and balance=0的数据,修改状态为0
"update tablename set status=0, time="当前时间" where status=1 and number=0;
mysql:关闭事务
以上是 tp5事务回滚操作多表 的全部内容, 来源链接: utcz.com/z/516132.html