- $transaction= Yii::app()->db->beginTransaction();//创建事务
- $transaction->commit();//提交事务
- $transaction->rollback();//回滚事务
- $transaction = Yii::app()->db->beginTransaction();
- try {
- $a = XXX::model()->findByPk(X);
- $a->x = 1;
- $a->update();
- $b = YYY::model()->findByPk(Y);
- $b->y = 2;
- $b->update();
- $transaction->commit(); //提交事务会真正的执行数据库操作
- } catch (Exception $e) {
- $transaction->rollback(); //如果操作失败, 数据回滚
- }