Skip to content

Commit

Permalink
Fixes creocoder#15: Fixed saving translations
Browse files Browse the repository at this point in the history
  • Loading branch information
greeflas committed Mar 12, 2018
1 parent 7ff9d04 commit ae5300c
Showing 1 changed file with 12 additions and 1 deletion.
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

0 comments on commit ae5300c

Please sign in to comment.