Thinkphp 跨模型实现事务回滚
程序员文章站
2022-05-11 17:21:35
数据库中数据表类型Innodb controller 层文件 $m = M(); $m->startTrans(); $shop = $m->query('SELECT * FROM `think_shop` WHERE id='.$custom['shop_id'].' FOR UPDATE'); ......
数据库中数据表类型Innodb
controller 层文件
$m = M();
$m->startTrans();
$shop = $m->query('SELECT * FROM `think_shop` WHERE id='.$custom['shop_id'].' FOR UPDATE');
$shop = $shop['0'];
$ret1 = D('Transaction')->goodsNum(参数1,参数2, $m);
if(!$ret1) {
$m->rollback();
} else {
$m->commit();
}
model 层文件
public function goodsNum(参数1,参数2, $m) {
操作....
//$ret1 = $m->execute("UPDATE `think_shop_goods` SET `stock` = stock-".$goodsNum[$key]." WHERE goods_id=".$goodsId." AND shop_id = ".$shop['id']);
//判断结果 返回true 或者 false
if(! $ret1) {
return false;
} else {
return true;
}
}
注意model层不需commit和rollback