手册里说得非常清楚 :
5.3.19 事务支持
thinkphp提供了单数据库的事务支持,如果要在应用逻辑中使用事务,可以参考下面的方法:
启动事务:
$user->starttrans()
提交事务:
$user->commit()
事务回滚:
$user->rollback()
事务是针对数据库本身的,所以可以跨模型操作的 。
例如:
// 在user模型中启动事务 $user->starttrans() // 进行相关的业务逻辑操作 $info = m("info"); // 实例化info对象 $info->save($user); // 保存用户信息 if (操作成功){ // 提交事务 $user->commit() }else{ // 事务回滚 $user->rollback() }
indexcontroller.class.php:
<?php namespace sms\controller; use think\controller; class indexcontroller extends controller { public function index(){ $data['operator'] = 'testss'; m()->starttrans(); $result = m('feehistory')->add($data); $result1 = $result2 = true; if(!empty($result)){ $regdeldata['level'] = '111'; $result1 = m('regdel')->add($regdeldata); $regdata['level'] = '101'; $result2 = m('reg')->where("registrycode='13693536752-sjb-huax-12345678'")->save($regdata); } if(!empty($result) && !empty($result1) && !empty($result2) ){ m()->commit(); //$this->success('事物提交',__root__); echo '事物提交'; }else{ m()->rollback(); //$this->error('事物回滚',__root__); echo '事物回滚'; } } }
相关推荐:php使用文本统计访问量的方法_php技巧
php学习笔记之基础知识
php+mysql实现的二级联动菜单效果详解_php技巧
以上就是thinkphp 3.2.2事务操作的方法的详细内容。
