/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = BlogTag::find();
     $query->orderBy(['frequency' => SORT_DESC]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'frequency' => $this->frequency]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
示例#2
0
 public static function findTagWeights($limit = 20)
 {
     $query = BlogTag::find()->orderBy(['frequency' => SORT_DESC]);
     $cacheId = Yii::$app->params["domain"] . '_blog_tag_' . md5(serialize($query));
     $models = Yii::$app->cache->get($cacheId);
     if ($models === false) {
         $models = $query->all();
         Yii::$app->cache->set($cacheId, $models, 3600);
     }
     $total = 0;
     foreach ($models as $model) {
         $total += $model->frequency;
     }
     $tags = [];
     if ($total > 0) {
         foreach ($models as $model) {
             $tags[$model->name] = 8 + (int) (16 * $model->frequency / ($total + 10));
         }
         ksort($tags);
     }
     return $tags;
 }