YII transaction on multiple model Can you provide me example of yii transaction on multiple model Like user model and post model
1 Answer
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.