Commit e3798abbbb33b593f17db30e0149cc6400b8af97
1 parent
1c6871b2
bug fix with trait
Showing
3 changed files
with
52 additions
and
8 deletions
Show diff stats
backend/traits/AliasableTrait.php
| @@ -14,20 +14,64 @@ namespace backend\traits; | @@ -14,20 +14,64 @@ namespace backend\traits; | ||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | use artbox\core\models\Alias; | 16 | use artbox\core\models\Alias; |
| 17 | -use artbox\core\models\traits\AliasableTrait as CoreTrait; | ||
| 18 | - | 17 | +use artbox\core\models\Language; |
| 19 | 18 | ||
| 20 | trait AliasableTrait | 19 | trait AliasableTrait |
| 21 | { | 20 | { |
| 22 | 21 | ||
| 23 | - | ||
| 24 | - use CoreTrait { | ||
| 25 | - CoreTrait::getAliases as coreGetAliases; | ||
| 26 | - } | 22 | + |
| 27 | 23 | ||
| 28 | public function getAliases() | 24 | public function getAliases() |
| 29 | { | 25 | { |
| 30 | return $this->hasMany(Alias::className(), ['route' => 'route'])->orderBy(['id' => SORT_ASC]); | 26 | return $this->hasMany(Alias::className(), ['route' => 'route'])->orderBy(['id' => SORT_ASC]); |
| 31 | } | 27 | } |
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * @return \yii\db\ActiveQuery | ||
| 31 | + */ | ||
| 32 | + public function getAlias() | ||
| 33 | + { | ||
| 34 | + return $this->hasOne(Alias::className(), [ 'route' => 'route' ]) | ||
| 35 | + ->where( | ||
| 36 | + [ | ||
| 37 | + 'language_id' => Language::getCurrent()->id, | ||
| 38 | + ] | ||
| 39 | + ); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * @return array|\yii\db\ActiveRecord[] | ||
| 44 | + */ | ||
| 45 | + public function loadAliases() | ||
| 46 | + { | ||
| 47 | + $langs = Language::find() | ||
| 48 | + ->where( | ||
| 49 | + [ | ||
| 50 | + 'status' => true, | ||
| 51 | + ] | ||
| 52 | + ) | ||
| 53 | + ->asArray() | ||
| 54 | + ->all(); | ||
| 55 | + | ||
| 56 | + if ($this->isNewRecord) { | ||
| 57 | + $aliases = []; | ||
| 58 | + foreach ($langs as $lang) { | ||
| 59 | + $aliases[] = new Alias( | ||
| 60 | + [ | ||
| 61 | + 'language_id' => $lang['id'], | ||
| 62 | + ] | ||
| 63 | + ); | ||
| 64 | + } | ||
| 65 | + return $aliases; | ||
| 66 | + } else { | ||
| 67 | + return $this->getAliases() | ||
| 68 | + ->all(); | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * @return string | ||
| 74 | + */ | ||
| 75 | + abstract public function getRoute(); | ||
| 32 | 76 | ||
| 33 | } | 77 | } |
frontend/controllers/BlogController.php
| @@ -51,7 +51,7 @@ | @@ -51,7 +51,7 @@ | ||
| 51 | )->with(['comments' => function (ActiveQuery $query){ | 51 | )->with(['comments' => function (ActiveQuery $query){ |
| 52 | $query->andWhere(['status' => true]); | 52 | $query->andWhere(['status' => true]); |
| 53 | }]) | 53 | }]) |
| 54 | - ->joinWith('language') | 54 | + ->with(['language','alias']) |
| 55 | ->where([ 'blog_article.status' => true ]) | 55 | ->where([ 'blog_article.status' => true ]) |
| 56 | ->distinct(), | 56 | ->distinct(), |
| 57 | 'pagination' => [ | 57 | 'pagination' => [ |
frontend/views/blog/_article.php
| @@ -18,7 +18,7 @@ $imageHeight = 240; | @@ -18,7 +18,7 @@ $imageHeight = 240; | ||
| 18 | <meta itemprop="headline" content="<?= $model->language->title ?>"/> <!-- ХХХ название статьи в H1 --> | 18 | <meta itemprop="headline" content="<?= $model->language->title ?>"/> <!-- ХХХ название статьи в H1 --> |
| 19 | <div class="img-blog-list"> | 19 | <div class="img-blog-list"> |
| 20 | <div itemprop="logo" itemscope itemtype="https://schema.org/ImageObject"> | 20 | <div itemprop="logo" itemscope itemtype="https://schema.org/ImageObject"> |
| 21 | - <a href="<?= Url::to(['alias' => $model->language->alias]) ?>"> | 21 | + <a href="<?= Url::to(['alias' => $model->alias]) ?>"> |
| 22 | <!--360x240--> | 22 | <!--360x240--> |
| 23 | 23 | ||
| 24 | <?php | 24 | <?php |