if($_POST['ModelA'])
{
/*开启事务机制*/
$transaction = Yii::app()->db->beginTransaction();
try
{
$validated = true;
/*此处省略一堆逻辑*/
$valid = $modelA->save();
$validated = $valid & $validated;
/*此处继续省略一堆逻辑*/
$valid = $modelB->save();
$validated = $valid & $validated;
/* 成功则 commit */
if($validated)
{
$transaction->commit();
$this->redirect(array('view','id'=>$model->id));
}
else
{
/*不成功即回滚 */
$transaction->rollBack();
}
}
catch(Exception $e)
{
$transaction->rollBack();
}
}