From bac425e927e2d2183a3f09d9ec530231bd97b54d Mon Sep 17 00:00:00 2001 From: Alban Jubert Date: Tue, 18 Jul 2017 11:32:33 +0200 Subject: [PATCH] Enh #17: Added a warning log in saveRelatedRecords method in case of exception --- CHANGELOG.md | 4 +++- src/SaveRelationsBehavior.php | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f9b628..6eca5c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,12 @@ # Yii2 Active Record Save Relations Behavior Change Log +## [1.2.1] +- Unreleased version + ## [1.2.0] ### Changed - Use of `ActiveQueryInterface` and `BaseActiveRecord` to ensure broader DB driver compatibility (Thx @bookin) - ## [1.1.3] 2017-03-04 ### Fixed - Bug #13: Relations as array were inserted then deleted instead of being updated diff --git a/src/SaveRelationsBehavior.php b/src/SaveRelationsBehavior.php index dd6da61..e140fa0 100644 --- a/src/SaveRelationsBehavior.php +++ b/src/SaveRelationsBehavior.php @@ -269,8 +269,10 @@ class SaveRelationsBehavior extends Behavior throw new Exception("One of the related model could not be validated"); } } catch (Exception $e) { + Yii::warning(get_class($e) . " was thrown during the saving of related records : " . $e->getMessage(), __METHOD__); 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__); } $event->isValid = false; // Stop saving, something went wrong return false; @@ -306,7 +308,10 @@ class SaveRelationsBehavior extends Behavior /** @var BaseActiveRecord $model */ $model = $this->owner; if (!is_null($relationModel) && ($relationModel->isNewRecord || count($relationModel->getDirtyAttributes()))) { - Yii::trace("Validating {$pettyRelationName} relation model", __METHOD__); +// if (key_exists($relationModel, $this->_relationsScenario)) { +// $relationModel->setScenario($this->_relationsScenario[$relationModel]); +// } + Yii::trace("Validating {$pettyRelationName} relation model using " . $relationModel->scenario . " scenario", __METHOD__); if (!$relationModel->validate()) { foreach ($relationModel->errors as $attributeErrors) { foreach ($attributeErrors as $error) {