Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/TranslateableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ class TranslateableBehavior extends Behavior
* @var string[] the list of attributes to be translated
*/
public $translationAttributes;

/**
* Temp buffer for entity relations.
*
* @var array
*/
protected $relationsBuffer = [];


/**
* @inheritdoc
Expand Down Expand Up @@ -83,6 +91,8 @@ public function getTranslation($language = null)

foreach ($translations as $translation) {
if ($translation->getAttribute($this->translationLanguageAttribute) === $language) {
$this->relationsBuffer[] = $translation;

return $translation;
}
}
Expand All @@ -93,6 +103,7 @@ public function getTranslation($language = null)
$translation = new $class();
$translation->setAttribute($this->translationLanguageAttribute, $language);
$translations[] = $translation;
$this->relationsBuffer = $translations;
$this->owner->populateRelation($this->translationRelation, $translations);

return $translation;
Expand Down Expand Up @@ -135,7 +146,7 @@ public function afterValidate()
public function afterSave()
{
/* @var ActiveRecord $translation */
foreach ($this->owner->{$this->translationRelation} as $translation) {
foreach ($this->relationsBuffer as $translation) {
$this->owner->link($this->translationRelation, $translation);
}
}
Expand Down