public function actionIndex($labelid = 1) { //查找所有的标签 $labels = Labels::find()->asArray()->all(); //根据标签ID查找问题 $label = Labels::find()->where(['labelid' => $labelid])->one(); $questions = $label->questions; //提问表 $session = Yii::$app->session; $askform = new AskForm(); //添加问题 if (!empty($session['user'])) { if ($askform->load(Yii::$app->request->post()) && $askform->validate()) { $ask = new Questions(); $ask->labelid = $labelid; $maxid = Questions::find()->where(['labelid' => $labelid])->orderBy('questionid desc')->one(); if (empty($maxid)) { $maxid = 1; } else { $maxid = $maxid['questionid']; } $ask->questionid = $maxid + 1; $ask->host = $session['username']; $ask->title = $askform->title; $ask->question = $askform->question; $ask->save(); return $this->redirect(Yii::$app->request->getUrl()); } } else { echo "未登录"; } return $this->render('index', ['labelid' => $labelid, 'labels' => $labels, 'questions' => $questions, 'askform' => $askform]); }
/** * Creates a new Questions model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Questions(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }