Summary of using database transactions in Yii
The Yii framework supports Database Transaction processing. For more information about transactions, see the article on this site:
About database (MySQL) Transactions
Transactions are generally used when batch deletion is encountered in a project. We will share a usage example below.
<? Php $ array = array (0 => array ('username' => 'phpernote. com_0 ', 'Password' => '000000'), 1 => array ('username' => 'U _ 1', 'Password' => '000000 '), 2 => array ('username' => 'U _ 2', 'Password' => '000000'); $ transaction = Yii: app () -> db-> dbConnection-> beginTransaction (); // here db represents the definition in main. php database connection object dbtry {Yii: app ()-> db-> createCommand ()-> insert ('tbl _ user', $ array [0]); Yii:: app ()-> db-> createCommand ()-> insert ('tbl _ user', $ array [1]); Yii: app () -> db-> createCommand ()-> insert ('tbl _ user', $ array [2]); $ transaction-> commit ();} catch (Exception $ e) {$ transaction-> rollback ();}
Note: If you are using a MySQL database, the table engine must be of the innodb type. Because the MyISAM engine of the MySQL database does not support transaction processing, the above Code will not achieve the expected purpose.
Articles you may be interested in
- Mysql database cache function analysis, debugging, and performance summary
- Yii database addition, modification, and deletion operations Summary
- Differences between database (MySQL) stored procedures and transactions
- About database (MySQL) Transactions
- Yii database query Operation Summary
- Summarize the causes and solutions for the slow MySQL Database Server
- In php, we use the curl post method to submit data and the get method to obtain webpage data.
- Case sensitivity of mysql Databases