YII: invalid things. HELPHELPHELPHELPHELPHELP. beginner PHP programmers.
Yii thing
Official statement:
$transaction=$connection->beginTransaction(); try { $connection->createCommand($sql1)->execute(); $connection->createCommand($sql2)->execute(); //.... other SQL executions $transaction->commit(); } catch(Exception $e) { $transaction->rollBack(); }
Self-encapsulated statement:
: $ This-> getDb () is the $ connection object obtained by the parent class through Yii.
I just uploaded the $ connection object to the Db layer during instantiation, and then added, deleted, modified, and queried operations on the Db layer.
But at the DB layer, when the excuse () method is executed, it is directly inserted into the database, and things do not work. solve the problem !!!
Service Layer
public function addCourseRecommend($courseRecommend,$payWayArray) { try{ $connection=$this->getDb(); $transaction=$connection->beginTransaction(); $this->courseRecommendDb= new CourseRecommendDb($connection); $this->payWayDb=new PayWayDb($connection); $this->courseRecommendDb->addCourseRecommend($courseRecommend); foreach($payWayArray as $tempPayWay) { $this->payWayDb->addPayWay($tempPayWay); $tempPayWayRecommend =new PayWayRecommend(); $this->payWayDb->addPayWayRecommend($tempPayWayRecommend); } $this->commit($transaction); }catch (Exception $e){ $this->rollback($transaction); $this->closeLink(); throw $e; } $this->closeLink(); }
Reply to discussion (solution)
Wait for a reply
People, people .....
But do not understand what your code is doing
However, you must note that the transaction is only valid in the same connection.
Yii databases are based on PDO, and new PDO creates new connections every time (this is different from the database extension of discrete functions)
But do not understand what your code is doing
However, you must note that the transaction is only valid in the same connection.
Yii databases are based on PDO, and new PDO creates new connections every time (this is different from the database extension of discrete functions)
Thank you for the solution.
In this way, I want to call the databases of multiple businesses in the service to implement things. The idea should be correct. the specific error has been solved.
After troubleshooting, the problem was found to be a database engine problem. I don't know how to change the table type to MyISAM.
MyISAM and InnoDB explain that InnoDB and MyISAM are the two most common table types when many people use MySQL. the Two table types have their own advantages and disadvantages, depending on the specific application. The basic difference is that the MyISAM type does not support advanced processing such as transaction processing, while the InnoDB type does. MyISAM tables emphasize performance, and the execution speed is faster than that of InnoDB tables.
After a long morning of hard work, we also considered that it may be a database engine problem at the beginning, but we ignored it when the possibility was low. we should be careful later !!!!