Home  >  Article  >  Backend Development  >  Yii中的数据库事务的使用方法小结_PHP教程

Yii中的数据库事务的使用方法小结_PHP教程

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

Yii中的数据库事务的使用方法小结

Yii框架是支持数据库事务处理的,关于事务,这里就不多说了,想要了解的读者可以参看本站文章:

关于数据库(MySQL)事务

在项目中遇到批量删除的地方一般会使用到事务,下面列举一个用法实例与大家分享。

<?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();
}

注意:如果你使用的是MySQL数据库,那么表引擎必须是innodb类型的,因为MySQL数据库的MyISAM引擎不支持事务处理,所以如上代码不会达到预想的目的。

您可能感兴趣的文章

  • Mysql 数据库缓存cache功能分析,调试以及性能总结
  • yii 数据库添加,修改,删除相关操作总结
  • 数据库(MySQL)存储过程和事务的区别
  • 关于数据库(MySQL)事务
  • yii数据库查询操作总结
  • 总结MySQL数据库服务器逐渐变慢的原因和解决办法
  • 在php中分别使用curl的post提交数据的方法和get获取网页数据的方法总结
  • 关于mysql数据库大小写敏感的问题

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/991979.htmlTechArticleYii中的数据库事务的使用方法小结 Yii框架是支持数据库事务处理的,关于事务,这里就不多说了,想要了解的读者可以参看本站文章: 关...
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