/** * Creates a new Event model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Event(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->renderAjax('create', ['model' => $model]); } }
/** * Creates a new Event model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($date) { $model = new Event(); $model->created_date = $date; if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->renderAjax('create', ['model' => $model]); } }
public function actionDataTables() { $responseType = Yii::$app->request->get('type'); $returnData = []; switch ($responseType) { case "fetch": $returnData = Event::find()->select(['id', 'title', 'description', 'start', 'end'])->where(['created_by' => Yii::$app->user->id])->orderBy(['id' => SORT_DESC])->all(); return Json::encode($returnData); case "crud": $requestAction = Yii::$app->request->post('action'); switch ($requestAction) { case "create": $requestID = key(Yii::$app->request->post('data')); $requestData = Yii::$app->request->post('data')[$requestID]; $model = new Event(); //块赋值 $model->attributes = $requestData; if (!$model->validate()) { $fieldErrors = []; foreach ($model->errors as $name => $status) { $fieldErrors[] = ['name' => $name, 'status' => Json::encode($status)]; } return Json::encode(['fieldErrors' => $fieldErrors, 'data' => []]); } if ($model->save()) { $requestData['id'] = $model->id; $data = []; $data[] = $requestData; return Json::encode(['data' => $data]); } else { $error = '恢复操作发生意外!'; throw new Exception($error); } case "edit": $editID = array_keys(Yii::$app->request->post('data')); //执行事务,保存必须都成功了才行 $transaction = Event::getDb()->beginTransaction(); //返回值 $data = []; try { foreach ($editID as $requestID) { $requestData = Yii::$app->request->post('data')[$requestID]; $model = Event::findOne($requestID); //块赋值 $model->attributes = $requestData; if (!$model->validate()) { $fieldErrors = []; foreach ($model->errors as $name => $status) { $fieldErrors[] = ['name' => $name, 'status' => Json::encode($status)]; } return Json::encode(['fieldErrors' => $fieldErrors, 'data' => []]); } $model->save(); //单个字段更新的时候 if (count($requestData) == 1 || !is_array($requestData)) { $requestData = Event::find()->select(['id', 'title', 'description', 'start', 'end'])->where(['id' => $requestID])->one(); } else { $requestData['id'] = $requestID; } $data[] = $requestData; } //提交事务 $transaction->commit(); return Json::encode(['data' => $data]); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } case "remove": //多选则删除全部 用deleteAll()不会触发 event: EVENT_BEFORE_DELETE 和 EVENT_AFTER_DELETE foreach (Yii::$app->request->post('data') as $removeID => $removeData) { Event::findOne($removeID)->delete(); } return Json::encode($returnData); default: return Json::encode($returnData); } default: return $this->render('_data-tables'); } }