Пример #1
0
 public function actionAnswer($labelid = 1, $questionid = 1)
 {
     $session = Yii::$app->session;
     //获取问题及相关答案
     $question = Questions::find()->where(['labelid' => $labelid, 'questionid' => $questionid])->one();
     $answers = $question->answers;
     $answerform = new AnswerForm();
     if ($answerform->load(Yii::$app->request->post()) && $answerform->validate() && !empty($session['user'])) {
         //插入回答
         $answer = new Answers();
         $answer->labelid = $labelid;
         $answer->questionid = $questionid;
         $maxanswerid = Answers::find()->where(['labelid' => $labelid, 'questionid' => $questionid])->orderBy('answerid desc')->one();
         $maxanswerid = $maxanswerid['answerid'];
         if (empty($maxanswerid)) {
             $maxanswerid = 0;
         }
         $answer->answerid = $maxanswerid + 1;
         $answer->answerer = $session['username'];
         $answer->answer = $answerform->answer;
         $answer->save();
         return $this->redirect(Yii::$app->request->getUrl());
     }
     return $this->render('answer', ['answerform' => $answerform, 'question' => $question, 'answers' => $answers]);
 }
Пример #2
0
 public function getAnswers()
 {
     return Answers::with("question")->get();
 }
Пример #3
0
 public function getAnswers()
 {
     return $this->hasMany(Answers::className(), ['labelid' => 'labelid', 'questionid' => 'questionid'])->orderBy('answerid')->asArray();
 }