/** * Creates a new Diary model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Diary(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('diaryCreate', ['model' => $model]); } }
/** * 保存创建和修改的信息 */ public function actionSave() { try { $post = Yii::$app->request->post(); $id = isset($post['diary_id']) ? $post['diary_id'] : ''; if (!empty($id)) { $model = $this->findModel($id); $diaryContentModel = DiaryContent::find()->andWhere(['diary_id' => $id])->one(); if (empty($diaryContentModel)) { $diaryContentModel = new DiaryContent(); } if ($model->load($post) && $model->save()) { $diary_id = $id; } } else { $model = new Diary(); $diaryContentModel = new DiaryContent(); if ($model->load($post) && $model->save()) { $diary_id = $model->id; } } if (empty($diary_id)) { throw new \Exception('插入日记失败'); } $post['DiaryContent']['diary_id'] = $diary_id; if (!($diaryContentModel->load($post) && $diaryContentModel->save())) { throw new \Exception('插入日记主题内容失败'); } } catch (\Exception $exp) { $message = ''; $message .= $exp->getMessage(); $message .= Yii::$app->getErrorHandler()->exception->getMessage(); $result = ['statusCode' => '300', 'message' => $message, 'navTabId' => 'diary_diary_index_id', 'forwardUrl' => '', 'callbackType' => '']; echo Json::encode($result); exit; } $result = ['statusCode' => '200', 'message' => '操作成功', 'navTabId' => 'diary_diary_index_id', 'forwardUrl' => '', 'callbackType' => 'closeCurrent']; echo Json::encode($result); exit; }