/** * 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; }
/** * 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.'); } }
/** * 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 safeUp() { $authors = Author::find()->all(); $authorsIds = []; foreach ($authors as $author) { $authorsIds[] = $author->id; } $fakerRus = Factory::create('ru_RU'); for ($i = 0; $i < 40; $i++) { $time = $fakerRus->unixTime; $book = new Book(); $book->name = $fakerRus->sentence(4); $book->date_create = $time; $book->date_update = $time; $book->date = $fakerRus->date; $book->author_id = $fakerRus->randomElement($authorsIds); $image = $fakerRus->image(Yii::getAlias('@frontend') . '/web/images'); $book->preview = str_replace(Yii::getAlias('@frontend') . '/web', '', $image); $result = $book->save(false); if (!$result) { return false; } } }
/** * 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; }
/** * @return \yii\db\ActiveQuery */ public function getBooks() { return $this->hasMany(Book::className(), ['author_id' => 'id']); }