コード例 #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Category::find()->joinWith('translations');
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]);
     $this->load($params);
     if (!$this->validate()) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'visible' => $this->visible, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     if (isset($this->parent_id) && $this->parent_id > 1) {
         $parent = Category::findOne((int) $this->parent_id);
         $query->andWhere(['>', 'left_border', $parent->left_border]);
         $query->andWhere(['<', 'right_border', $parent->right_border]);
     }
     $query->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'description', $this->description]);
     return $dataProvider;
 }
コード例 #2
0
ファイル: Category.php プロジェクト: yeesoft/yii2-yee-post
 /**
  * Return all categories.
  *
  * @param bool $asArray
  *
  * @return static[]
  */
 public static function getCategories($skipCategories = [])
 {
     $result = [];
     $categories = Category::findOne(1)->getDescendants()->joinWith('translations')->all();
     foreach ($categories as $category) {
         if (!in_array($category->id, $skipCategories)) {
             $result[$category->id] = str_repeat('   ', $category->depth - 1) . $category->title;
         }
     }
     return $result;
 }