/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); if ($model->load(Yii::$app->request->post()) && $model->save()) { /** @var Category[] $categories */ $categories = Category::find()->where(['id' => $model->categories_id])->all(); foreach ($categories as $category) { $model->link('categories', $category); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'category_names' => ArrayHelper::map(Category::find()->all(), 'id', 'name')]); } }
public function actionNew() { $postModel = new Post(); $category = new Category(); $categoryList = ArrayHelper::map($category->find()->asArray()->all(), 'id', 'title'); $postList = $postModel->find()->asArray()->all(); $message = ''; if ($postModel->load(Yii::$app->request->post()) && $postModel->save()) { $selectedCategories = Category::find()->where(['id' => $postModel->categoryIds])->all(); foreach ($selectedCategories as $category) { $postModel->link('categories', $category); } $message = 'Category added success'; } return $this->render('new', ['model' => $postModel, 'message' => $message, 'categoryList' => $categoryList, 'postList' => $postList]); }
public function actionEdit($id) { $model = Moderation::findOne((int) $id); if (!$model) { throw new Exception("Запись не найдена!"); } if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post(), $model->formName()) && $model->validate() && !Yii::$app->request->post('nosave', false)) { $post = new Post(); $post->attributes = $model->attributes; $post->visible = true; $tags = Yii::$app->request->post($model->formName(), ['tags' => []])['tags']; if ($post->save()) { if (!empty($tags)) { foreach ($tags as $tag) { $modelTag = Tag::add($tag); if ($modelTag) { $post->link('tags', $modelTag); } } } $model->delete(); // публикация в твиттере ContentGenerator::Twitter($post); return $this->redirect('moderate'); } } return $this->render('_edit', ['model' => $model]); }