/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Book::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->setSort(['attributes' => ['id', 'name', 'authorFullName' => ['asc' => ['authors.firstname' => SORT_ASC], 'desc' => ['authors.firstname' => SORT_DESC], 'label' => Yii::t('book', 'authorFullName')], 'date', 'date_create']]); $this->load($params); if (!$this->validate()) { $query->joinWith(['author']); return $dataProvider; } $query->andFilterWhere(['author_id' => $this->author_id]); $dateFromArray = explode("/", $this->date_from); $dateForm = count($dateFromArray) == 3 ? $dateFromArray[2] . "-" . $dateFromArray[1] . "-" . $dateFromArray[0] : ''; $dateToArray = explode("/", $this->date_to); $dateTo = count($dateToArray) == 3 ? $dateToArray[2] . "-" . $dateToArray[1] . "-" . $dateToArray[0] : ''; $query->andFilterWhere(['like', 'name', $this->name]); if ($dateForm && $dateTo) { $query->andFilterWhere(['BETWEEN', 'date', $dateForm, $dateTo]); } else { if ($dateForm && !$dateTo) { $query->andFilterWhere(['>=', 'date', $dateForm]); } else { if (!$dateForm && $dateTo) { $query->andFilterWhere(['<=', 'date', $dateTo]); } } } $query->joinWith(['author' => function ($q) { }]); return $dataProvider; }
/** * Lists all Book models. * * @return mixed */ public function actionIndex() { $searchModel = new BookSearch(); if (null !== Yii::$app->request->get('BookSearch')) { $dataProvider = $searchModel->search(Yii::$app->request->queryParams); } else { $dataProvider = new ActiveDataProvider(['query' => Book::find()->with(['author'])]); } return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Book::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $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(['id' => $this->id, 'author_id' => $this->author_id]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['between', 'date', $this->date1, $this->date2]); return $dataProvider; }
public function actionMatch() { $arrResult = []; Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $id = Yii::$app->request->post('id'); $model = Book::find()->where(['id' => $id])->one(); if ($model) { $curDownloads = (int) $model->download; $model->download = $curDownloads + 1; $model->save(); $arrResult['result'] = 'ok'; } else { $arrResult['result'] = 'error'; } return $arrResult; }
/** * 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, 'sort' => ['defaultOrder' => ['date_create' => SORT_DESC]]]); unset($dataProvider->sort->attributes['preview']); $dataProvider->sort->attributes['author.name'] = ['asc' => ['authors.firstname' => SORT_ASC, 'authors.lastname' => SORT_ASC], 'desc' => ['authors.firstname' => SORT_DESC, 'authors.lastname' => 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]); $query->andFilterWhere(['between', 'date', $this->date_from, $this->date_to]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Book::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $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(['id' => $this->id, 'date_create' => $this->date_create, 'date_update' => $this->date_update, 'date' => $this->date, 'author_id' => $this->author_id]); if (isset($params['BookSearch']['from_date']) && preg_match('/^(\\d{2})\\.(\\d{2})\\.(\\d{4})$/', $params['BookSearch']['from_date'], $matches)) { $query->andWhere(['>=', 'date', $matches[3] . '-' . $matches[2] . '-' . $matches[1]]); } if (isset($params['BookSearch']['to_date']) && preg_match('/^(\\d{2})\\.(\\d{2})\\.(\\d{4})$/', $params['BookSearch']['to_date'], $matches)) { $query->andWhere(['<=', 'date', $matches[3] . '-' . $matches[2] . '-' . $matches[1]]); } $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'preview', $this->preview]); return $dataProvider; }