Ejemplo n.º 1
0
 public function search($params)
 {
     $query = Book::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['name' => SORT_ASC]]]);
     // join with relation `whose` that is a relation to the table `user`
     // and set the table alias to be `whose`
     $query->joinWith(['author' => function ($query) {
         $query->from(['author' => 'tz_author']);
     }]);
     // enable sorting for the related column
     $dataProvider->sort->attributes['author.last_name'] = ['asc' => ['author.last_name' => SORT_ASC], 'desc' => ['author.last_name' => SORT_DESC]];
     // load the search form data and validate
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     // adjust the query by adding the filters
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'author.last_name', $this->getAttribute('author.last_name')])->andFilterWhere(['between', 'date', $this->date ? Book::getIntDate($this->date) : $this->date, $this->after_date ? Book::getIntDate($this->after_date) : $this->after_date]);
     return $dataProvider;
 }
Ejemplo n.º 2
0
 /**
  * Updates an existing Book model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $oldImage = $model->preview;
     if ($model->load(Yii::$app->request->post())) {
         $image = UploadedFile::getInstance($model, 'preview');
         if (!empty($image)) {
             unlink(Yii::$app->params['uploadPath'] . $oldImage);
             $temp_var = explode(".", $image->name);
             $ext = end($temp_var);
             $model->preview = Yii::$app->security->generateRandomString() . ".{$ext}";
             $path = Yii::$app->params['uploadPath'] . $model->preview;
             $image->saveAs($path);
         }
         $model->date = Book::getIntDate($model->date);
         $model->save();
         $session = Yii::$app->session;
         if (!$session->isActive) {
             $session->open();
         }
         $session->set('needSearchParam', true);
         return $this->redirect(['index']);
     } else {
         return $this->render('update', ['model' => $model, 'author' => Author::find()->all()]);
     }
 }