在一个事务中执行ActiveRecords的两种不同方法


execute two different methods of ActiveRecords in one transactions

有任何可能的方法为活动记录方法定义事务吗?我有两个名为Answer和QuizMark的活动记录,它们有两种方法。我想说服务器将它们作为事务来执行。这是我的代码:

public function actionChangeAnswersStatus($formId, $userId) {
        if (isset($formId) && isset($userId)) {
            Answer::changeIgnoreStatus($formId, $userId);
            QuizMark::changeIgnoreStatus($formId, $userId);
            echo 'quizes';
        }
    }

有什么可能的解决方案吗?

尝试:

$connection = $this->connectDb();
$transaction = $connection->beginTransaction();
// You commands
$transaction->commit();
// Note: if an error occurs, call rollBack...
$transaction->rollBack();