public function actionEntry() { //关闭页面布局 $this->layout = false; //创建表单模型 $entry = new Entry(); //如果是Post请求提交 if (Yii::$app->request->getIsPost()) { //表单模型设置属性为post值 $entry->setAttributes(Yii::$app->request->post()); //表单模型数据验证 if ($entry->validate()) { //正确即保存数据 $result = $entry->save(); //返回保存后的信息 if ($result) { var_dump($entry->attributes); } else { var_dump($entry->getErrors()); } } else { //返回错误提示 var_dump($entry->getErrors()); } } else { //如果不是Post请求,正常显示模板 return $this->render('entry', ['model' => $entry]); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store($logbook_id) { $logbook = Logbook::findOrFail($logbook_id); if ($logbook->user_id == Auth::user()->id or $logbook->user_id == 0) { $entry = new Entry(Input::only('title', 'body', 'started_at', 'finished_at', 'evidence_id', 'who', 'what', 'where', 'which', 'way', 'when', 'why')); $entry->logbook_id = $logbook->id; if ($entry->validate()) { $entry->save(); } else { return View::make('entries.create', ['entry' => $entry, 'logbook' => $logbook])->withErrors($entry->validator()); } return Redirect::to(route('logbooks.show', [$logbook->id]))->with('message', ['content' => 'Entry met succes aangemaakt!', 'class' => 'success']); } else { return Redirect::to(route('logbooks.show', [$logbook->id]))->with('message', ['content' => 'Geen rechten om entry weg te schrijven!', 'class' => 'danger']); } }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Entry(); $model->owner_by = Yii::app()->user->getId(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Entry'])) { $model->attributes = $_POST['Entry']; if ($model->validate()) { $fileName = $this->getAndSaveUploadedFile($model); if ($fileName) { $model->imagepath = $fileName; } if ($model->save(false)) { $this->redirect(array('view', 'id' => $model->id)); } } } $this->render('create', array('model' => $model)); }