From 5efd1da9855a293ee85c154438294b760be89990 Mon Sep 17 00:00:00 2001 From: Catirau Mihail Date: Fri, 14 Aug 2015 10:59:39 +0300 Subject: [PATCH 1/2] Auto populate translation attributes With this changes will automatically populate translation attributes Now we can use default CRUD controllers actions without any changes: public actionCreate() { $model = new Post(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id ]); } else { return $this->render('create', [ 'model' => $model, ]); } } public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id ]); } else { return $this->render('update', [ 'model' => $model, ]); } } --- src/TranslateableBehavior.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/TranslateableBehavior.php b/src/TranslateableBehavior.php index 251fb20..c2b509e 100644 --- a/src/TranslateableBehavior.php +++ b/src/TranslateableBehavior.php @@ -41,6 +41,8 @@ class TranslateableBehavior extends Behavior public function events() { return [ + ActiveRecord::EVENT_INIT => 'addTranslations', // populate translations on new object + ActiveRecord::EVENT_AFTER_FIND => 'addTranslations', // populate translations on find object ActiveRecord::EVENT_AFTER_VALIDATE => 'afterValidate', ActiveRecord::EVENT_AFTER_INSERT => 'afterSave', ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave', @@ -118,6 +120,27 @@ public function hasTranslation($language = null) return false; } + + /** + * Auto populate translation attributes + * + * @return void + */ + public function addTranslations() + { + $this->owner->{$this->translationRelation}; + + /* @var ActiveRecord $class */ + $class = $this->owner->getRelation($this->translationRelation)->modelClass; + + /* If method create or update - populate attributes */ + $className = (new \ReflectionClass($this->translationClass))->getShortName(); + foreach (Yii::$app->request->post($className, []) as $language => $data) { + foreach ($data as $attribute => $translation) { + $this->owner->translate($language)->$attribute = $translation; + } + } + } /** * @return void From eaf1e7825a42ad8387685f86cbb6d3a9863552ae Mon Sep 17 00:00:00 2001 From: Catirau Mihail Date: Fri, 14 Aug 2015 11:03:27 +0300 Subject: [PATCH 2/2] Update addTranslations --- src/TranslateableBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TranslateableBehavior.php b/src/TranslateableBehavior.php index c2b509e..8c4ac61 100644 --- a/src/TranslateableBehavior.php +++ b/src/TranslateableBehavior.php @@ -134,7 +134,7 @@ public function addTranslations() $class = $this->owner->getRelation($this->translationRelation)->modelClass; /* If method create or update - populate attributes */ - $className = (new \ReflectionClass($this->translationClass))->getShortName(); + $className = (new \ReflectionClass($class))->getShortName(); foreach (Yii::$app->request->post($className, []) as $language => $data) { foreach ($data as $attribute => $translation) { $this->owner->translate($language)->$attribute = $translation;