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)






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?


 /* @var $db CDbConnection */

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

	/* @var $transaction CDbTransaction */

	$transaction = $db->beginTransaction();

	try {




	} catch (Exception $ex) {





Thank you Spyros.