The following code is a typical example of using transactions (note that some DBMS may not support transactions):
$transaction = $connection->beginTransaction(); try { $connection->createCommand($sql1)->execute(); $connection->createCommand($sql2)->execute(); //.... other SQL executions $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); throw $e; }