/**
* thinkkphp 5.0 模型和Db類混合使用事務操作
*/
public function test(){
Db::startTrans();
$test = new Test();
$re1 = $test->save([
'admin_id' => '1',
],['id' => 1]);
$re2 = Db::name('test')->where("id=2")->update(['admin_id'=>2]);
if($re1 && $re2){
Db::commit();
echo 'success1';
}else{
Db::rollback();
echo "fail1";
}
}
一直以爲模型和Db類不能混合使用事務,測試後才發現原來可以。