public function actionCreate() { $pad = new Pad(); if ($pad->load(Yii::$app->request->post()) && $pad->validate()) { Yii::$app->user->identity->link('pads', $pad); Yii::$app->session->setFlash('success', 'Pad is successfully created.'); return $this->redirect('note/list'); } return $this->render('create', ['pad' => $pad]); }
/** * Get pad or raise 404 * * @param integer $id pad id * @return Pad * @throws \yii\web\HttpException */ private function getPad($id) { $pad = Pad::findOne(['id' => $id, 'user_id' => Yii::$app->user->identity->getId()]); if (!$pad) { throw new \yii\web\HttpException(404, 'Not Found'); } return $pad; }
public function delete($id, Request $request, Pad $padModel) { $pad = $this->padExists($id); if (!$pad) { return $response->redirect('/pads'); } $pad->delete(); $request->session->add(['success' => 'Pad deleted successfuly.']); return app('twig')->render('pads/index.htm', ['pads' => $padModel->all()]); }
public function update($id, Request $request, Note $noteModel, Pad $padModel) { $note = $this->noteExists($id); if (!$note) { return $response->redirect('/notes'); } $pads = $padModel->where('user_id', $request->user()->id)->get(); $rules = ['name' => 'required', 'text' => 'required', 'pad' => 'required|exists:pads,id']; $validator = app('validation')->make($request->all(), $rules); if ($validator->fails()) { $request->session->add(['errors' => $validator->errors()->all()]); return app('twig')->render('notes/edit.htm', ['note' => $note, 'pads' => $pads]); } $note->name = $request->input('name'); $note->text = $request->input('text'); $note->pad_id = $request->input('pad'); $note->save(); $request->session->add(['success' => 'Note updated successfuly.']); return app('twig')->render('notes/edit.htm', ['note' => $note, 'pads' => $pads]); }
/** * Get pad * * @return app\models\Pad */ public function getPad() { return $this->hasOne(Pad::className(), ['id' => 'pad_id']); }
/** * Get pads */ public function getPads() { return $this->hasMany(Pad::className(), ['user_id' => 'id']); }