I can’t create transactions on multiple Active Records.
Example 1. Don’t execute catch block:
$transaction = Yii::app()->db->beginTransaction();
$model = User::model()->findAll();
try {
$i=0;
foreach (model as $row){
if($i++==1){
$row->status = 'A';//can be only integer, must rollback!!!
} else {
$row->status = '1';
}
$row->save();
}
$transaction->commit();
} catch (Exception $ex) {
$transaction->rollback();
die('FAILED!!!');
}
Example 2. Execute catch block, but save all rows in DB, execpt when $i==1:
$transaction = Yii::app()->db->beginTransaction();
$model = User::model()->findAll();
try {
$i=0;
foreach (model as $row){
if($i++==1){
$row->status = 'A';//can be only integer, must rollback!!!
} else {
$row->status = '1';
}
if(!$row->save()){
$transaction->rollback();
}
}
$transaction->commit();
} catch (Exception $ex) {
die('FAILED!!!');
}
I tried also some other solutions, but without any success. What I am doing wrong and how it is possible to rollback rows that is already saved if one of rows can’t save?