DhirendraBisht
DhirendraBisht

Reputation: 110

YII transaction on multiple model

YII transaction on multiple model Can you provide me example of yii transaction on multiple model Like user model and post model

Upvotes: 2

Views: 2223

Answers (1)

hamed
hamed

Reputation: 8033

This is the sample of using transactions with models:

$transaction = Yii::app()->db->beginTransaction();
try
{
    $post= new Post;
    //set attributes
    $post->save();
    $user = new User;
    //set attributes;
    $user->save();
    $transaction->commit();
 }
 catch(Exception $e)
 {
    $transaction->rollBack();
 }

When you use transaction, all codes inside try block considered as one transction and if saving of each record fail for any reason, transaction rolls back. For more information about transactions and Active Record you can follow this link.

Upvotes: 7

Related Questions