Models and Transactions

I have two models in a master/detail relationship, message and message_recipient. I am doing a two-stage save. Simplified, it is like this:




if ( $message->save() ) }

    $message_recipient->save();

}



How does one handle transactions for this in Yii? Would this work?




$transaction = $connection->beginTransaction();

try {

    if ( $message->save() ) }

        $message_recipient->save();

    }

    $transaction->commit();

}

catch(Exception $e) {

    $transaction->rollBack();

}



Or does the model->save() method already have built-in transaction processing?

Thanks in advance for any leads to documents or examples.

Judging from the docs and the posts I read in forum, this is the way it should work.

By default no transaction is created.