From 7c4d4f23072bb83a9da986caa88c6a3028084a10 Mon Sep 17 00:00:00 2001 From: fps01 Date: Fri, 1 Apr 2016 12:48:48 +0300 Subject: [PATCH] table name fix --- src/traits/MultilingualTrait.php | 33 +++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/traits/MultilingualTrait.php b/src/traits/MultilingualTrait.php index b644f2d..9914bf9 100644 --- a/src/traits/MultilingualTrait.php +++ b/src/traits/MultilingualTrait.php @@ -9,6 +9,23 @@ trait MultilingualTrait { + protected static $translationTableName; + + /** + * @return string + */ + protected static function getTranslationTableName() + { + if (self::$translationTableName === null) { + /** @var ActiveRecord|MultilingualActiveRecord $model */ + $model = new static; + /** @var ActiveRecord $translationModelClassName */ + $translationModelClassName = $model->getTranslationModelClassName(); + self::$translationTableName = $translationModelClassName::tableName(); + } + return self::$translationTableName; + } + /** * @return ActiveQuery * @throws \yii\base\InvalidConfigException @@ -19,31 +36,25 @@ public static function find() $query = Yii::createObject(ActiveQuery::className(), [get_called_class()]); $query = $query ->innerJoinWith(['defaultTranslation']); - /** @var ActiveRecord|MultilingualActiveRecord $modelInstance */ if (method_exists(get_called_class(), 'applyDefaultScope')) { $query = call_user_func([get_called_class(), 'applyDefaultScope'], $query); } else { + /** @var ActiveRecord|MultilingualActiveRecord $modelInstance */ $modelInstance = new self; if ($modelInstance->translationPublishedAttribute !== false) { /** @var ActiveRecord $translationModelClassName */ $translationModelClassName = $modelInstance->getTranslationModelClassName(); - - $tableName = $translationModelClassName::tableName(); - + self::$translationTableName = $translationModelClassName::tableName(); // add condition on $where = [ - "{$tableName}.{$modelInstance->translationPublishedAttribute}" => + self::$translationTableName . '.' . $modelInstance->translationPublishedAttribute => $modelInstance->translationPublishedAttributeValue ]; - unset($modelInstance); - $query = $query->where($where); } } - - return $query; } @@ -54,7 +65,7 @@ public function getDefaultTranslation() { /** @var \yii\db\ActiveRecord|\DevGroup\Multilingual\behaviors\MultilingualActiveRecord $this */ return $this->hasOne($this->getTranslationModelClassName(), ['model_id' => 'id']) - ->where(['language_id' => Yii::$app->multilingual->language_id]); + ->where([static::getTranslationTableName() . '.language_id' => Yii::$app->multilingual->language_id]); } /** @@ -66,4 +77,4 @@ public function getTranslations() return $this->hasMany($this->getTranslationModelClassName(), ['model_id' => 'id']); } -} \ No newline at end of file +}