/** * 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; }
/** * @return \yii\db\ActiveQuery */ public function getProducts() { return $this->hasMany(Products::className(), ['id' => 'product_id'])->viaTable('categories_has_products', ['category_id' => 'id']); }
/** * Finds the Products model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Products the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Products::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }