Yii数据库操作——事务管理

dbConnection是CDbConnection的实例
官方文档

$model = Post::model();
$transaction = $model->dbConnection->beginTransaction();
try{
	$post = $model->findByPk(10);
	$post->title = 'new post title';
	$post->save();
	$transaction->commit();
} catch (Exception $e){
	$transaction->rollback();
}



实际项目

$trans = Yii::app()->db->beginTransaction();
try {
	$manufacturer = new Manufacturer();	
	$manufacturer->name = $name;
	$manufacturer->email = $email;
	$manufacturer->save();
	$trans->commit();
} catch (Exception $e) {
	$trans->rollback();
	$this->response(array('status' => 1, 'msg' => $e->getMessage()));	
}



其实使用的时候跟凡客体的我是凡客或淘宝体的亲一样。

注:Yii::app()后面的db在../config/main.php中已配置

'components'=>array(
	'user'=>array('allowAutoLogin'=>true,),
	'db'=>array("数据库连接参数"),
)
posted @ 2014-02-07 10:26  维优网络工作室  阅读(128)  评论(0)    收藏  举报