/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Book::find(); $query->joinWith(['author']); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 5], 'sort' => ['defaultOrder' => ['date_create' => SORT_DESC, 'title' => SORT_ASC]]]); $dataProvider->setSort(['attributes' => ['id', 'name' => ['asc' => ['name' => SORT_ASC], 'desc' => ['name' => SORT_DESC], 'label' => "Book's Title"], 'authorName' => ['asc' => ['authors.id' => SORT_ASC], 'desc' => ['authors.id' => SORT_DESC], 'label' => 'Author'], 'date' => ['asc' => ['date' => SORT_ASC], 'desc' => ['date' => SORT_DESC], 'label' => 'Date published'], 'date_create' => ['asc' => ['date_create' => SORT_ASC], 'desc' => ['date_create' => SORT_DESC], 'label' => 'Date create']]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'date_create' => $this->date_create, 'date_update' => $this->date_update, 'date' => $this->date]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'author_id', $this->author_id])->andFilterWhere(['>', 'date', $this->date_begin])->andFilterWhere(['<', 'date', $this->date_end]); return $dataProvider; }
/** * Логика поиска * @param $params * @return ActiveDataProvider */ public function search($params) { $query = Book::find()->joinWith('author'); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => self::PAGE_SIZE], 'sort' => ['attributes' => ['name', 'author_id' => ['asc' => ['firstname' => SORT_ASC, 'lastname' => SORT_ASC], 'desc' => ['firstname' => SORT_DESC, 'lastname' => SORT_DESC], 'default' => SORT_DESC]]]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $this->addCondition($query, 'name', true); $this->addCondition($query, 'author_id'); if ($this->date_from && $this->date_to) { $query->andFilterWhere(['between', 'date', $this->date_from, $this->date_to]); } elseif ($this->date_from && !$this->date_to) { $query->andFilterWhere(['>=', 'date', $this->date_from]); } elseif (!$this->date_from && $this->date_to) { $query->andFilterWhere(['<=', 'date', $this->date_to]); } if ($this->date_from || $this->date_to) { $query->addOrderBy('date'); } return $dataProvider; }