Home  >  Article  >  Backend Development  >  Summary of how to use database transactions in Yii_PHP tutorial

Summary of how to use database transactions in Yii_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:55:24874browse

A summary of how to use database transactions in Yii

The Yii framework supports database transaction processing. I won’t say much about transactions here. Readers who want to know more can refer to Articles on this site:

About database (MySQL) transactions

Transactions are generally used when batch deletions are encountered in projects. Here is a usage example to share with you.

<?php
$array=array(
	0=>array('username'=>'phpernote.com_0','password'=>'123456'),
	1=>array('username'=>'u_1','password'=>'123456'),
	2=>array('username'=>'u_2','password'=>'123456')
);
$transaction=Yii::app()->db->dbConnection->beginTransaction();
//此处db代表的是定义在main.php中的数据库连接对象db
try{
	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 cache function analysis, debugging and performance summary
  • yii database addition, modification, deletion related operations Summary
  • The difference between database (MySQL) stored procedures and transactions
  • About database (MySQL) transactions
  • Yii database query operation summary
  • Summary of MySQL database server Reasons and solutions for gradual slowdown
  • A summary of the method of using curl's post to submit data and get to obtain web page data in PHP
  • About the case sensitivity of the MySQL database

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/991979.htmlTechArticle Summary of how to use database transactions in Yii The Yii framework supports database transaction processing. Regarding transactions, we will not discuss them here. Enough to say, readers who want to know more can refer to the article on this site: Guan...
Statement:
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