예제 #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Articles::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, 'create_at' => $this->create_at]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'content', $this->content]);
     return $dataProvider;
 }
예제 #2
0
 /**
  * Updates an existing Articles 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);
     $aticleCategory = new CategoryArticle();
     $articleImg = Articles::find()->with('articleImg')->where(['id' => $id])->all();
     $selected = CategoryArticle::find()->where(['article_id' => $id])->all();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         if (Yii::$app->request->post('CategoryArticle')) {
             $count = count(Yii::$app->request->post('CategoryArticle')['category_id']);
             CategoryArticle::deleteAll(['article_id' => $id]);
             for ($i = 0; $i < $count; ++$i) {
                 $category = new CategoryArticle();
                 $category->article_id = $id;
                 $category->category_id = Yii::$app->request->post('CategoryArticle')['category_id'][$i];
                 $category->save();
             }
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model, 'aticleCategory' => $aticleCategory, 'img' => $articleImg[0]['articleImg'], 'selected' => $selected]);
     }
 }