public static function outTop8Links() { $topTags = TopTag::find()->orderBy(['id' => SORT_DESC])->limit(8)->all(); for ($i = 0; $i < count($topTags) - 1; $i++) { for ($j = $i + 1; $j < count($topTags); $j++) { $first = $topTags[$i]; $second = $topTags[$j]; if (strcmp($first->tag->name, $second->tag->name) < 0) { $temp = $topTags[$i]; $topTags[$i] = $topTags[$j]; $topTags[$j] = $temp; } } } foreach ($topTags as $tag) { $tagName = str_replace(' ', '+', $tag->name); ?> <a href="<?php echo Url::to(['site/search', 't' => $tagName]); ?> " class="tag"><?php echo $tag->name; ?> </a> <?php } ?> <a href="<?php echo Url::to(['site/tags']); ?> " class="tag">Все теги</a> <?php }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = TopTag::find(); $topTagTable = TopTag::tableName(); $tagTable = Tag::tableName(); $query->joinWith(['tag' => function ($query) use($tagTable) { $query->from(['tag' => $tagTable]); }]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]); // enable sorting for the related columns $addSortAttributes = ["tag.name"]; foreach ($addSortAttributes as $addSortAttribute) { $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]]; } if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'tag_id' => $this->tag_id]); $query->andFilterWhere(['like', 'tag.name', $this->getAttribute('tag.name')]); return $dataProvider; }