Skip to content

Commit ae5300c

Browse files
committed
Fixes #15: Fixed saving translations
1 parent 7ff9d04 commit ae5300c

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/TranslateableBehavior.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ class TranslateableBehavior extends Behavior
3434
* @var string[] the list of attributes to be translated
3535
*/
3636
public $translationAttributes;
37+
38+
/**
39+
* Temp buffer for entity relations.
40+
*
41+
* @var array
42+
*/
43+
protected $relationsBuffer = [];
44+
3745

3846
/**
3947
* @inheritdoc
@@ -83,6 +91,8 @@ public function getTranslation($language = null)
8391

8492
foreach ($translations as $translation) {
8593
if ($translation->getAttribute($this->translationLanguageAttribute) === $language) {
94+
$this->relationsBuffer[] = $translation;
95+
8696
return $translation;
8797
}
8898
}
@@ -93,6 +103,7 @@ public function getTranslation($language = null)
93103
$translation = new $class();
94104
$translation->setAttribute($this->translationLanguageAttribute, $language);
95105
$translations[] = $translation;
106+
$this->relationsBuffer = $translations;
96107
$this->owner->populateRelation($this->translationRelation, $translations);
97108

98109
return $translation;
@@ -135,7 +146,7 @@ public function afterValidate()
135146
public function afterSave()
136147
{
137148
/* @var ActiveRecord $translation */
138-
foreach ($this->owner->{$this->translationRelation} as $translation) {
149+
foreach ($this->relationsBuffer as $translation) {
139150
$this->owner->link($this->translationRelation, $translation);
140151
}
141152
}

0 commit comments

Comments
 (0)