Heim  >  Artikel  >  Backend-Entwicklung  >  YII 事物无效有关问题 HELP HELP HELP HELP HELP HELP !

YII 事物无效有关问题 HELP HELP HELP HELP HELP HELP !

WBOY
WBOYOriginal
2016-06-13 12:01:22894Durchsuche

YII 事物无效问题 HELP HELP HELP HELP HELP HELP !!!!!!
各位大神们 入门PHP程序员求帮助

Yii事物

官方写法:
 

<br />       $transaction=$connection->beginTransaction();<br />        try<br />        {<br />            $connection->createCommand($sql1)->execute();<br />            $connection->createCommand($sql2)->execute();<br />            //.... other SQL executions<br />            $transaction->commit();<br />        }<br />        catch(Exception $e)<br />        {<br />            $transaction->rollBack();<br />        }<br />


自己封装的写法:
说明:$this->getDb()是父类通过Yii 获取到的$connection对象
我只是在实例化的时候把$connection对象传到Db层,然后Db层进行增删改查等操作
但是在DB层的时候,执行excuse()方法时候直接插入到数据库了,事物没起作用,求解!!!
service层
<br /> public function addCourseRecommend($courseRecommend,$payWayArray)<br />    {<br />        try{<br />            $connection=$this->getDb();<br />            $transaction=$connection->beginTransaction();<br />            $this->courseRecommendDb= new CourseRecommendDb($connection);<br />            $this->payWayDb=new PayWayDb($connection);<br />            $this->courseRecommendDb->addCourseRecommend($courseRecommend);<br />            foreach($payWayArray as $tempPayWay)<br />            {<br />                $this->payWayDb->addPayWay($tempPayWay);<br />                $tempPayWayRecommend =new PayWayRecommend();<br />                $this->payWayDb->addPayWayRecommend($tempPayWayRecommend);<br />            }<br />            $this->commit($transaction);<br />        }catch (Exception $e){<br />            $this->rollback($transaction);<br />            $this->closeLink();<br />            throw $e;<br />        }<br />        $this->closeLink();<br />    }<br /><br />

------解决方案--------------------
但不明白你的代码在做什么
但需要注意的是:事务只在同一连接中有效
Yii 的数据库是基于 PDO 的,每次 new PDO 都会产生新的连接(这一点是与分立函数的数据库扩展是不同的)

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn