Skip to content

Instantly share code, notes, and snippets.

@yjeroen
Created January 4, 2012 14:22
Show Gist options
  • Save yjeroen/1560242 to your computer and use it in GitHub Desktop.
Save yjeroen/1560242 to your computer and use it in GitHub Desktop.
I think I got the solution!
<?php
class foo extends CActiveRecord
{
public function save($runValidation=true,$attributes=null)
{
if( Yii::app()->db->getCurrentTransaction() === null )
{
$transaction = Yii::app()->db->beginTransaction();
$transaction->attachbehavior('modelSaveTransaction', new CBehavior);
tr('This transaction is owned by an IcsActiveRecord->save');
}
$currentTransaction = Yii::app()->db->getCurrentTransaction();
if (!isset($currentTransaction->modelSaveTransaction))
{
tr('This save is called in an outside transaction, not owned by IcsActiveRecord->save.');
if (!isset($currentTransaction->firstSaveIsCalled))
{
$currentTransaction->attachbehavior('firstSaveIsCalled', new CBehavior);
$startedTheSave = true;
}
}
try {
parent::save($runValidation, $attributes);
if(!empty($this->relationsHolder))
$this->relatedSave($runValidation); //this function also calls foo::save()
if($this->hasErrors())
{
throw new CDbException('Model(s) do not pass validation.');
}
else
{
if(isset($transaction))
$transaction->commit();
return true;
}
}
catch (Exception $e)
{
if(isset($transaction))
$transaction->rollback();
else if (isset($startedTheSave))
throw $e;
tr('Class: '.get_class($this) .'. '.'Exception in save() catched! '.$e->getMessage());
return false;
}
}
}
####################################################
# CONTROLLER A works
public function actionUpdateA($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Incident']))
{
$model->attributes=$_POST['Incident'];
$model->setRelatedData('kanalen');
$model->setRelatedData('dienstenVerstoord');
$model->setRelatedData('nrns');
$model->setRelatedData('postcodes');
$model->setRelatedData('plaatsen');
$model->setRelatedData('overigeLocaties');
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
# CONTROLLER B works
public function actionUpdateB($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Incident']))
{
$model->attributes=$_POST['Incident'];
$model->setRelatedData('kanalen');
$model->setRelatedData('dienstenVerstoord');
$model->setRelatedData('nrns');
$model->setRelatedData('postcodes');
$model->setRelatedData('plaatsen');
$model->setRelatedData('overigeLocaties');
$transaction = Yii::app()->db->beginTransaction();
try {
if($model->save())
$this->redirect(array('view','id'=>$model->id));
} catch (Exception $e)
{
$transaction->rollback();
tr('Catched by controller');
}
}
$this->render('update',array(
'model'=>$model,
));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment