From 3409c652b3a48b9be86d8ea4355d58d0cd60b507 Mon Sep 17 00:00:00 2001 From: Alban Jubert Date: Wed, 8 Nov 2017 13:37:50 +0100 Subject: [PATCH] Fix for transaction being triggered multiple times for nested relations and detection of the transaction support for the ActiveRecord being used --- src/SaveRelationsBehavior.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/SaveRelationsBehavior.php b/src/SaveRelationsBehavior.php index 950fa62..6df06bb 100644 --- a/src/SaveRelationsBehavior.php +++ b/src/SaveRelationsBehavior.php @@ -261,9 +261,14 @@ class SaveRelationsBehavior extends Behavior */ protected function saveRelatedRecords(BaseActiveRecord $model, ModelEvent $event) { - if (($model->isNewRecord && $model->isTransactional($model::OP_INSERT)) - || (!$model->isNewRecord && $model->isTransactional($model::OP_UPDATE)) - || $model->isTransactional($model::OP_ALL) + if ( + method_exists($model, 'isTransactional') + && is_null($model->getDb()->transaction) + && ( + ($model->isNewRecord && $model->isTransactional($model::OP_INSERT)) + || (!$model->isNewRecord && $model->isTransactional($model::OP_UPDATE)) + || $model->isTransactional($model::OP_ALL) + ) ) { $this->_transaction = $model->getDb()->beginTransaction(); }