Transaction across models

Hi

Is there a way to use the same transaction across models?

The example in the docs work for one model but I’d love to use a transaction across models.

The alternative seems to be plain SQL but I was wondering if it exists without raw SQL.

Thanks a lot

[color="#C0C0C0"]i making the MultilingualBehavior, and then finish, i will release to the

Extensions

http://www.yiiframework.com/extensions/[/color]

[color="#FF0000"]it’s my mistake, sorry! [/color]

Transaction

look up as:

Translation

I’m confused.

Is your extension related to transaction? Multilingualbehavior is a bit misleading as far as wording.

The transactions will always cover multiple models until you commit the transaction. I’m not sure how you are starting them, but you can do something like this and it should work over multiple models:




// Start the transaction

$transaction = Yii::app()->db->beginTransaction();

try {

    // Do the updates

    FirstModel::model()->update(array(...));

    SecondModel::model()->update(array(...));


    // Commit the transaction

    $transaction->commit();

}

// Was there an error?

catch (Exception $e) {

    // Error, rollback transaction

    $transaction->rollback();

}



Is that what you are trying to do?

Yes! Thank you so much.

Is it OK to specify one particular model? Will the transaction still check all models before committing?

That is…

Does it matter if you do:


$transaction = Yii::app()->db->beginTransaction();

vs:


$model = new MyModel;

$transaction = $model->dbConnection->beginTransaction();

However you do it… in the end its the current connection object (CDbConnection) that begins the transaction