Skip to content

Commit b4f0e9b

Browse files
author
voskobovich
committed
Fixed the logic of creating a updater
1 parent 8c4fbed commit b4f0e9b

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/LinkerBehavior.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,31 @@ class LinkerBehavior extends Behavior implements LinkerBehaviorInterface
8181
*/
8282
private $dynamicFieldsOfModel = [];
8383

84+
/**
85+
* Build updater instance
86+
* @param $relationConfig
87+
* @param $defaultUpdaterClass
88+
* @return object
89+
*/
90+
private function buildUpdater($relationConfig, $defaultUpdaterClass)
91+
{
92+
if (!is_array($relationConfig)) {
93+
$relationConfig = [$relationConfig];
94+
}
95+
96+
if (empty($relationConfig['updater']['class'])) {
97+
if (!empty($relationConfig['updater'])) {
98+
$relationConfig['updater']['class'] = $defaultUpdaterClass;
99+
} else {
100+
$relationConfig['updater'] = [
101+
'class' => $defaultUpdaterClass
102+
];
103+
}
104+
}
105+
106+
return Yii::createObject($relationConfig['updater']);
107+
}
108+
84109
/**
85110
* Events list
86111
* @return array
@@ -159,11 +184,8 @@ public function saveRelations()
159184

160185
if (!empty($relation->via) && $relation->multiple) {
161186
// Many-to-many
162-
if (empty($dynamicAttributeParams['updater']['class'])) {
163-
$dynamicAttributeParams['updater']['class'] = ManyToManyUpdater::className();
164-
}
165187

166-
$updater = Yii::createObject($dynamicAttributeParams['updater']);
188+
$updater = $this->buildUpdater($dynamicAttributeParams, ManyToManyUpdater::className());
167189
if (!$updater instanceof ManyToManyUpdaterInterface) {
168190
throw new InvalidConfigException(
169191
'Updater class must implement ' .
@@ -172,11 +194,8 @@ public function saveRelations()
172194
}
173195
} elseif (!empty($relation->link) && $relation->multiple) {
174196
// One-to-many on the many side
175-
if (empty($dynamicAttributeParams['updater']['class'])) {
176-
$dynamicAttributeParams['updater']['class'] = OneToManyUpdater::className();
177-
}
178197

179-
$updater = Yii::createObject($dynamicAttributeParams['updater']);
198+
$updater = $this->buildUpdater($dynamicAttributeParams, OneToManyUpdater::className());
180199
if (!$updater instanceof OneToManyUpdaterInterface) {
181200
throw new InvalidConfigException(
182201
'Updater class must implement ' .

tests/_data/Book.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public function behaviors()
7676
'linkerBehavior' => [
7777
'class' => LinkerBehavior::className(),
7878
'relations' => [
79-
'author_ids' => ['authors'],
80-
'review_ids' => ['reviews'],
79+
'author_ids' => 'authors',
80+
'review_ids' => 'reviews',
8181
]
8282
]
8383
];

0 commit comments

Comments
 (0)