| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace common\components;
- use Yii;
- use yii\base\Component;
- use yii\db\Transaction as DbTransaction;
- class Transaction extends Component{
-
- /**
- * 开启一个事务,不嵌套,多次调用只在一个事务中,兼容mycat
- * @Author bing
- * @DateTime 2021-10-14 10:59:56
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return DbTransaction
- */
- public $dirver;//数据库【mycat mysql】
- private $level=0;
- private $transaction;
- public function start(){
- if($this->dirver == 'mysql') return Yii::$app->db->beginTransaction();
- $this->transaction = Yii::$app->db->getTransaction();
- if($this->transaction){
- $this->transaction = Yii::$app->db->beginTransaction();
- }
- $this->level ++;
- return $this;
- // $level = Yii::$app->db->getTransaction()->getLevel();
- }
- public function rollback(){
- $this->level --;
- if($this->level === 0){
- $this->transaction->rollback();
- return;
- }
- }
- public function commit(){
- $this->level --;
- if($this->level === 0){
- $this->transaction->commit();
- }
- }
- public function getLevel(){
- $this->transaction->commit();
- return $this->level;
- }
- }
|