search
Homephp教程php手册Yii中的数据库事务的使用方法小结

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数据库大小写敏感的问题
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software