public static function find() { return parent::find()->where([Product::tableName() . '.isdel' => 0]); }
/** * Finds the Product model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Product the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Product::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = $this->find(); $query->joinWith(['product', 'order']); $dataProvider = new ActiveDataProvider(['query' => $query]); /* uncomment to sort by relations table on respective column */ $dataProvider->sort->attributes['productTitle'] = ['asc' => [Product::tableName() . '.title' => SORT_ASC], 'desc' => [Product::tableName() . '.title' => SORT_DESC]]; $dataProvider->sort->attributes['orderReference'] = ['asc' => [Order::tableName() . '.reference' => SORT_ASC], 'desc' => [Order::tableName() . '.reference' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $params = self::queryNumber([['id', $this->tableName()], ['product_id'], ['order_id'], ['amount'], ['quantity'], ['isdel']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryString([['data']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryTime([['time', $this->tableName()]]); foreach ($params as $p) { $query->andFilterWhere($p); } /* example to use search all in field1,field2,field3 or field4 if ($this->term) { $query->andFilterWhere(["OR","lower(field1) like '%".strtolower($this->term)."%'", ["OR","lower(field2) like '%".strtolower($this->term)."%'", ["OR","lower(field3) like '%".strtolower($this->term)."%'", "lower(field4) like '%".strtolower($this->term)."%'" ] ] ]); } */ $query->andFilterWhere(["like", "lower(" . Product::tableName() . ".title)", strtolower($this->productTitle)]); $query->andFilterWhere(["like", "lower(" . Order::tableName() . ".reference)", strtolower($this->orderReference)]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getProduct() { return $this->hasOne(Product::className(), ['id' => 'product_id']); }