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

WBOY
Release: 2016-06-13 12:01:22
Original
906 people have browsed it

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 />
Copy after login


自己封装的写法:
说明:$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 />
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!