one transaction for two models

Hellow everyone.

I have the following code:




if(isset($_GET['uchid']) && isset($_GET['pid']))

{

  $pred = $_GET['pid'];

  $uchen = Uchni::model()->findByPk($_GET['uchid']);

  $predmet = Predmety::model()->find("uchen_id=:uchen_id and $pred = 1",array(':uchen_id'=>$_GET['uchid']));

				

  if($uchen && $predmet)

  {

	$uchen->delete();

	$predmet->delete();

  }

}



It’s very important for my application that when $uchen is deleted $predmet is deleted as well, otherwise my app will crash. How can I make sure that records from both tables are deleted simultaniously?

Is it possible to use transaction here?

Transaction is exacly what you need!

check the doc about how to do it.

Yes but in docs the transaction is used for one model. How to use one transaction for two models like in my case?




<?php

 /* @var $db CDbConnection */

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

	/* @var $transaction CDbTransaction */

	$transaction = $db->beginTransaction();

	try {

	$model1->delete();

	$model2->delete();

 	$transaction->commit();

	} catch (Exception $ex) {

  	$transaction->rollback();

  	

	}

?>



Thank you Spyros.