Open zebraf1 opened 6 years ago
My current fix is to extend this behavior class and extend afterSave method:
public function afterSave()
{
$translations = $this->owner->{$this->translationRelation};
$this->owner->populateRelation($this->translationRelation, []); // clear related to avoid double relations
/* @var ActiveRecord $translation */
foreach ($translations as $translation) {
$this->owner->link($this->translationRelation, $translation);
}
}
Note that your class name has a spelling error. Translateable is not a correct word, see https://en.wiktionary.org/wiki/translatable
I have a model with translatable behaviour and
When I do:
$mainModel->translations has duplicate entries
This code loops over existing translations and calls link() which causes duplicate related objects:
If $this->owner->{$this->translationRelation} returns related objects, they are already linked. When you create new translations, you have to link them yourself.