Skip to content

Fixes #15: Fixed saving translations #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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