Yii数据库操作——事务管理
作者:zccst
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("数据库连接参数"),
)
如果您觉得本文的内容对您的学习有所帮助,您可以微信:
分享到:
评论