Skip to content

Commit

Permalink
table name fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fps01 committed Apr 1, 2016
1 parent 7f8fad3 commit 7c4d4f2
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/traits/MultilingualTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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]);
}

/**
Expand All @@ -66,4 +77,4 @@ public function getTranslations()
return $this->hasMany($this->getTranslationModelClassName(), ['model_id' => 'id']);
}

}
}

0 comments on commit 7c4d4f2

Please sign in to comment.