コード例 #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Products::find()->alias('Products');
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => array('defaultOrder' => ['id' => SORT_DESC])]);
     $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;
     }
     // grid filtering conditions
     $query->andFilterWhere(['Products.id' => $this->id, 'Products.base_price' => $this->base_price, 'Products.active' => $this->active]);
     if ($this->showNonCat == 1) {
         $query->joinWith('categories');
         $query->andWhere(new Expression('categories.id is NULL'));
     }
     $query->andFilterWhere(['like', 'Products.title', $this->title])->andFilterWhere(['like', 'Products.description', $this->description])->andFilterWhere(['like', 'Products.code', $this->code]);
     return $dataProvider;
 }
コード例 #2
0
 /**
  * Lists all Products models.
  * @return mixed
  */
 public function actionIndex($id = 64, $size = null)
 {
     if (!is_null($size)) {
         Yii::$app->session->set('pageSize', (int) $size);
     }
     $ids = [];
     if (!is_null($id)) {
         $ids = \Yii::$app->cache->get('child_categories_' . md5($id));
         if (!$ids) {
             $models = Categories::find()->active()->all();
             $models = Categories::tree($models, 0, [], (int) $id);
             $models = ArrayHelper::map($models, 'id', 'title');
             $ids = array_keys($models);
             $ids[] = (int) $id;
             \Yii::$app->cache->set('child_categories_' . md5($id), $ids);
         }
     }
     $size = in_array(Yii::$app->session->get('pageSize'), [5, 60, 120, 180]) ? Yii::$app->session->get('pageSize') : 60;
     $category = !is_null($id) ? $this->findCatModel($id) : null;
     \Yii::$app->view->registerMetaTag(['name' => 'description', 'content' => !is_null($category) ? $category->meta_description : \Yii::$app->params['description']]);
     \Yii::$app->view->registerMetaTag(['name' => 'keywords', 'content' => !is_null($category) ? $category->meta_keywords : \Yii::$app->params['keywords']]);
     $dataProvider = new ActiveDataProvider(['query' => Products::getDb()->cache(function ($db) use($ids) {
         return Products::find()->joinWith(['categories', 'images'])->where(empty($ids) ? [] : ['categories.id' => $ids])->active()->orderBy(new Expression('IFNULL(categories.title, "яя")'));
     }), 'sort' => array('defaultOrder' => ['id' => SORT_DESC]), 'pagination' => ['pageSize' => $size]]);
     return $this->render('index', ['dataProvider' => $dataProvider, 'category' => $category]);
 }