/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Book::find()->joinWith('author'); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->sort->attributes['author'] = ['asc' => ['books_authors.lastname' => SORT_ASC, 'books_authors.firstname' => SORT_ASC], 'desc' => ['books_authors.lastname' => SORT_DESC, 'books_authors.firstname' => 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; } $query->andFilterWhere(['author_id' => $this->author_id]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['>=', 'date', $this->dateFromSql])->andFilterWhere(['<=', 'date', $this->dateToSql]); return $dataProvider; }
/** * Finds the Book model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Book the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Book::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(Book::className(), ['author_id' => 'id']); }