naigo 2011-08-03 09:48:56 5143次浏览 0条回复 0 0 0

我这样使用事务,任意个无法成功提交都能回滚。

$connection = Yii::app()->db;
$transaction = $connection->beginTransaction(); 
try{
    $connection->createCommand('update sp_operator set suspend=0 
    where department_id='.$departmentModel->id)->execute();
    $connection->createCommand('update sp_department set suspend=0 
    where id='.$departmentModel->id)->execute();
    $transaction->commit();  
}
catch (Exception $e){
    $transaction->rollBack();
}

但是要求我用AR的事务,不让写sql语句,于是我这样写:

$connection = Yii::app()->db;
$transaction = $connection->beginTransaction(); 
    try{				                                                                         Operator::model()->updateAll(array('suspend'=>1),"department_id=".$departmentModel->id);
    Department::model()->updateAll(array('short_name'=>1),"id=".$departmentModel->id);
    $transaction->commit(); 
}
catch (Exception $e){
    $transaction->rollBack();
}

不管哪个出现问题都回滚不了。于是我又改成下面的写法

$departmentModel = new Department();
$transaction=$departmentModel->dbConnection->beginTransaction();
    try{				                                                                         Operator::model()->updateAll(array('suspend'=>1),"department_id=".$departmentModel->id);
    Department::model()->updateAll(array('short_name'=>1),"id=".$departmentModel->id);
    $transaction->commit(); 
}
catch (Exception $e){
    $transaction->rollBack();
}

结果还是回滚不了,请问当有两个AR实例的时候事务应该怎么写呢? PS:数据库是MySql的InnoDB

    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册