/** * 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, 'sort' => ['defaultOrder' => ['position' => SORT_ASC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'visible' => $this->visible, 'position' => $this->position]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'alias', $this->alias]); return $dataProvider; }
public function actionView($alias) { /** @var $category Category */ $category = Category::find()->where(['alias' => $alias])->one(); if (!$category) { throw new HttpException(404, 'Категория товара не найдена'); } $q = \Yii::$app->request->get('q'); if (!empty($q)) { $products = Product::find()->where(['categoryId' => $category->id])->visible()->andWhere(['like', 'title', $q])->all(); } else { $products = $category->products; } $route = $this->route; $title = 'Товары - Категории - ' . $category->title; $categoryList = Category::find()->with(['products'])->orderBy(['position' => SORT_ASC])->all(); return $this->render('view', ['category' => $category, 'products' => $products, 'q' => $q, 'route' => $route, 'title' => $title, 'categoryList' => $categoryList]); }