Example #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Category::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => false, 'sort' => new \yii\data\Sort(['attributes' => ['name']])]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'parent_id' => $this->parent_id]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     $query->andFilterWhere(['like', 'text', $this->text]);
     return $dataProvider;
 }
Example #2
0
 public static function buildTextTree($id = null, $level = 1, $ban = [])
 {
     $return = [];
     $prefix = str_repeat('--', $level);
     $level++;
     if (empty($id)) {
         $categories = Category::find()->where('parent_id = 0 OR parent_id is null')->orderBy('sort DESC')->asArray()->all();
     } else {
         $categories = Category::find()->where(['parent_id' => $id])->orderBy('sort DESC')->asArray()->all();
     }
     foreach ($categories as $category) {
         if (!in_array($category['id'], $ban)) {
             $return[$category['id']] = "{$prefix} {$category['name']}";
             $return = $return + self::buildTextTree($category['id'], $level, $ban);
         }
     }
     return $return;
 }