Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Removed transactional control
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrogehlen committed Aug 26, 2018
1 parent 8b7aedb commit 9ff39ad
Showing 1 changed file with 0 additions and 43 deletions.
43 changes: 0 additions & 43 deletions src/SaveRelationsBehavior.php
Expand Up @@ -324,7 +324,6 @@ public function beforeValidate(ModelEvent $event)
*/
protected function saveRelatedRecords(BaseActiveRecord $model, ModelEvent $event)
{
$this->startTransactionForModel($model);
try {
foreach ($this->_relations as $relationName) {
if (array_key_exists($relationName, $this->_oldRelationValue)) { // Relation was not set, do nothing...
Expand All @@ -344,38 +343,13 @@ protected function saveRelatedRecords(BaseActiveRecord $model, ModelEvent $event
}
} catch (Exception $e) {
Yii::warning(get_class($e) . ' was thrown while saving related records during beforeValidate event: ' . $e->getMessage(), __METHOD__);
$this->_rollback();
$model->addError($model->formName(), $e->getMessage());
$event->isValid = false; // Stop saving, something went wrong
return false;
}
return true;
}

/**
* @param BaseActiveRecord $model
*/
protected function startTransactionForModel(BaseActiveRecord $model)
{
if ($this->isModelTransactional($model) && is_null($model->getDb()->transaction)) {
$this->_transaction = $model->getDb()->beginTransaction();
}
}

/**
* @param BaseActiveRecord $model
* @return bool
*/
protected function isModelTransactional(BaseActiveRecord $model)
{
if (method_exists($model, 'isTransactional')) {
return ($model->isNewRecord && $model->isTransactional($model::OP_INSERT))
|| (!$model->isNewRecord && $model->isTransactional($model::OP_UPDATE))
|| $model->isTransactional($model::OP_ALL);
}
return false;
}

/**
* @param BaseActiveRecord $model
* @param ModelEvent $event
Expand Down Expand Up @@ -455,18 +429,6 @@ private function _prepareHasManyRelation(BaseActiveRecord $model, $relationName)
}
}

/**
* Rollback transaction if any
* @throws DbException
*/
private function _rollback()
{
if (($this->_transaction instanceof Transaction) && $this->_transaction->isActive) {
$this->_transaction->rollBack(); // If anything goes wrong, transaction will be rolled back
Yii::info('Rolling back', __METHOD__);
}
}

/**
* Set relation foreign keys that point to owner primary key
* @param $relationName
Expand Down Expand Up @@ -522,7 +484,6 @@ public function afterSave()
}
} catch (Exception $e) {
Yii::warning(get_class($e) . ' was thrown while saving related records during afterSave event: ' . $e->getMessage(), __METHOD__);
$this->_rollback();
/***
* Sadly mandatory because the error occurred during afterSave event
* and we don't want the user/developper not to be aware of the issue.
Expand All @@ -531,9 +492,6 @@ public function afterSave()
}
$owner->refresh();
$this->_relationsSaveStarted = false;
if (($this->_transaction instanceof Transaction) && $this->_transaction->isActive) {
$this->_transaction->commit();
}
}
}

Expand Down Expand Up @@ -718,7 +676,6 @@ public function afterDelete()
}
} catch (Exception $e) {
Yii::warning(get_class($e) . ' was thrown while deleting related records during afterDelete event: ' . $e->getMessage(), __METHOD__);
$this->_rollback();
throw $e;
}
}
Expand Down

0 comments on commit 9ff39ad

Please sign in to comment.