/** * 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(); $model->time = date("Y-m-d H:i:s"); $model->author_id = Yii::$app->user->id; $model->isdel = 0; if (Yii::$app->request->post()) { $post = Yii::$app->request->post(); $category = []; if (isset($post['Post']['category'])) { $category = $post['Post']['category']; } if (is_array($post['Post']['tags'])) { $post['Post']['tags'] = implode(",", $post['Post']['tags']); } $model->load($post); $transaction = Yii::$app->db->beginTransaction(); try { if ($model->save()) { $cs = BlogCatPos::deleteAll("post_id = :id", ["id" => $model->id]); foreach ($category as $d) { $c = new BlogCatPos(); $c->post_id = $model->id; $c->category_id = $d; $c->isdel = 0; $c->save(); } $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } else { $model->id = array_merge($category, []); $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); } } return $this->render('create', ['model' => $model]); }