/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Books::find()->with('author'); // add conditions that should always apply here //$this->date_from = '21321'; $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->setSort(['attributes' => ['id', 'author_id' => ['asc' => ['authors.firstname' => SORT_ASC, 'authors.lastname' => SORT_ASC], 'desc' => ['authors.firstname' => SORT_DESC, 'authors.lastname' => SORT_DESC], 'label' => 'Автор', 'default' => SORT_ASC], 'date', 'date_create', 'name']]); if (!($this->load($params) && $this->validate())) { $query->joinWith(['author']); return $dataProvider; } $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(['id' => $this->id, 'author_id' => $this->author_id]); $query->andFilterWhere(['like', 'name', $this->name]); if (!empty($this->dateFrom)) { $query->andFilterWhere(['>=', 'date', $this->dateFrom]); } if (!empty($this->dateTo)) { $query->andFilterWhere(['<=', 'date', $this->dateTo]); } return $dataProvider; }
/** * Finds the Books model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Books the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Books::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getBooks() { return $this->hasMany(Books::className(), ['author_id' => 'id']); }