Пример #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Books::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     /**
      * Настройка параметров сортировки
      * Важно: должна быть выполнена раньше $this->load($params)
      * statement below
      */
     $dataProvider->setSort(['attributes' => ['id', 'name', 'author' => ['asc' => ['lastname' => SORT_ASC, 'firstname' => SORT_ASC], 'desc' => ['lastname' => SORT_DESC, 'firstname' => SORT_DESC], 'default' => SORT_ASC], 'date', 'date_create']]);
     $query->joinWith(['authors']);
     if (!($this->load($params) && $this->validate())) {
         /**
          * Жадная загрузка данных модели Страны
          * для работы сортировки.
          */
         return $dataProvider;
     }
     $query->andFilterWhere(['books.id' => $this->id, 'author_id' => $this->author_id]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     // Фильтр по полному имени
     $query->andWhere('authors.firstname LIKE "%' . $this->author . '%" ' . 'OR authors.lastname LIKE "%' . $this->author . '%"');
     // Фильтр по дате
     /*
     // Если на форме нужно воодить как на плейсхолдере
     $myDateTime= DateTime::createFromFormat('d/m/Y', $this->date_begin);
     $myDateTime->format('Y-m-d');
     */
     $query->andFilterWhere(['>=', 'date', $this->date_begin]);
     $query->andFilterWhere(['<=', 'date', $this->date_end]);
     return $dataProvider;
 }
Пример #2
0
 /**
  * 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.');
     }
 }