From e3798abbbb33b593f17db30e0149cc6400b8af97 Mon Sep 17 00:00:00 2001 From: Anastasia Date: Thu, 23 Aug 2018 13:34:24 +0300 Subject: [PATCH] bug fix with trait --- backend/traits/AliasableTrait.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++------ frontend/controllers/BlogController.php | 2 +- frontend/views/blog/_article.php | 2 +- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/backend/traits/AliasableTrait.php b/backend/traits/AliasableTrait.php index 4675f60..ed6a960 100644 --- a/backend/traits/AliasableTrait.php +++ b/backend/traits/AliasableTrait.php @@ -14,20 +14,64 @@ namespace backend\traits; use artbox\core\models\Alias; -use artbox\core\models\traits\AliasableTrait as CoreTrait; - +use artbox\core\models\Language; trait AliasableTrait { - - use CoreTrait { - CoreTrait::getAliases as coreGetAliases; - } + public function getAliases() { return $this->hasMany(Alias::className(), ['route' => 'route'])->orderBy(['id' => SORT_ASC]); } + + /** + * @return \yii\db\ActiveQuery + */ + public function getAlias() + { + return $this->hasOne(Alias::className(), [ 'route' => 'route' ]) + ->where( + [ + 'language_id' => Language::getCurrent()->id, + ] + ); + } + + /** + * @return array|\yii\db\ActiveRecord[] + */ + public function loadAliases() + { + $langs = Language::find() + ->where( + [ + 'status' => true, + ] + ) + ->asArray() + ->all(); + + if ($this->isNewRecord) { + $aliases = []; + foreach ($langs as $lang) { + $aliases[] = new Alias( + [ + 'language_id' => $lang['id'], + ] + ); + } + return $aliases; + } else { + return $this->getAliases() + ->all(); + } + } + + /** + * @return string + */ + abstract public function getRoute(); } diff --git a/frontend/controllers/BlogController.php b/frontend/controllers/BlogController.php index ef81f58..4184968 100755 --- a/frontend/controllers/BlogController.php +++ b/frontend/controllers/BlogController.php @@ -51,7 +51,7 @@ )->with(['comments' => function (ActiveQuery $query){ $query->andWhere(['status' => true]); }]) - ->joinWith('language') + ->with(['language','alias']) ->where([ 'blog_article.status' => true ]) ->distinct(), 'pagination' => [ diff --git a/frontend/views/blog/_article.php b/frontend/views/blog/_article.php index 3b5ff09..f33d71d 100755 --- a/frontend/views/blog/_article.php +++ b/frontend/views/blog/_article.php @@ -18,7 +18,7 @@ $imageHeight = 240;
- +