/** * Lists all Books models. * @return mixed */ public function actionIndex() { $searchModel = new BooksSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); // Сохраним (или загрузим) в сессии параметры фильтров и сортировки if (Yii::$app->request->queryParams || Yii::$app->request->get('sort')) { Yii::$app->session->set('BookSearchUrl', Yii::$app->request->url); } else { if ($url = Yii::$app->session->get('BookSearchUrl')) { $this->redirect($url); } } return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]); }
/** * Lists all Books models. * @return mixed */ public function actionIndex() { $searchModel = new BooksSearch(); $model = new Books(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); //$author = Authors::find()->where(['id' => $model->id])->one(); //echo $author = $author->lastname." ".$author->firstname;die(); // id автора //$book = Books::find()->where(['id' => $id])->one(); //$author_id = $book->author_id; // ФИО автора по id //$author = Authors::find()->where(['id' => $author_id])->one(); //$author = $author->lastname." ".$author->firstname; return $this->render('index', ['model' => $model, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = BooksSearch::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['attributes' => ['id', 'name', 'preview', 'author_id', 'date', 'date_create', 'fullname']]]); $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->select(['CONCAT(authors.firstname, " ", authors.lastname) fullname', 'books.*'])->joinWith('author'); $query->andFilterWhere(['id' => $this->id, 'date_create' => $this->date_create, 'date_update' => $this->date_update, 'date' => $this->date, 'author_id' => $this->author_id]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'preview', $this->preview]); if (!$this->date_from && $this->date_to) { // Если нет даты "с", установим минимальную $date_from = '0000-01-01'; $date_to = Yii::$app->formatter->asDateSlash2Hyphen($this->date_to); } elseif ($this->date_from && !$this->date_to) { // Если нет даты "до", поставим сегодняшнюю, тк дата добавления не может быть из будущего $date_from = Yii::$app->formatter->asDateSlash2Hyphen($this->date_from); $date_to = date("Y-m-d"); } elseif ($this->date_from && $this->date_to) { $date_from = Yii::$app->formatter->asDateSlash2Hyphen($this->date_from); $date_to = Yii::$app->formatter->asDateSlash2Hyphen($this->date_to); } else { $date_from = ''; $date_to = ''; } $query->andFilterWhere(['between', 'date', $date_from, $date_to]); return $dataProvider; }
/** * Lists all Books models. * @return mixed */ public function actionIndex() { // $searchModel = new BooksSearch(); // $dataProvider = $searchModel->search(Yii::$app->request->queryParams); // // return $this->render('index', [ // 'searchModel' => $searchModel, // 'dataProvider' => $dataProvider, // ]); $searchModel = new BooksSearch(); if (Yii::$app->request->queryParams) { $books = $searchModel->search(Yii::$app->request->queryParams); } else { $books = $searchModel->getAllBooks(); } return $this->render('index', ['searchModel' => $searchModel, 'books' => $books]); }