Beispiel #1
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $post = new Post();
     $categories = Category::find()->orderBy('title')->all();
     if ($post->load(Yii::$app->request->post()) && $post->save()) {
         return $this->redirect(['update', 'id' => $post->id]);
     } else {
         return $this->render('create', ['post' => $post, 'categories' => $categories]);
     }
 }
Beispiel #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Post::find()->joinWith(['category']);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $dataProvider->sort->attributes['categoryTitle'] = ['asc' => ['category.category' => SORT_ASC], 'desc' => ['category.category' => SORT_DESC]];
     $this->load($params);
     if (!$this->validate()) {
         return $dataProvider;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'categoryId' => $this->categoryId, 'sort' => $this->sort, 'isVisible' => $this->isVisible, 'createdAt' => $this->createdAt, 'updatedAt' => $this->updatedAt]);
     $query->andFilterWhere(['like', 'post.alias', $this->alias])->andFilterWhere(['like', 'post.title', $this->title])->andFilterWhere(['like', 'category.title', $this->categoryTitle]);
     return $dataProvider;
 }
 /**
  * @return array
  * @param integer $id
  * @throws \yii\web\NotFoundHttpException
  */
 public function actionDelete($id)
 {
     /* @var $category Category */
     $category = $this->findModel(['id' => $id]);
     $this->performModelDelete($category, ['afterDelete' => function (Category $category) {
         Post::updateAll(['categoryId' => null], ['categoryId' => $category->id]);
         $category->updateAll(['parentId' => $category->parentId], ['parentId' => $category->id]);
     }, 'success' => function () {
         return ['message' => Yii::t('management', 'Category removed')];
     }, 'error' => function ($category) {
         return ['message' => $category->errors];
     }]);
 }