为了账号安全,请及时绑定邮箱和手机立即绑定

Yii2-测验-使用多对多表

Yii2-测验-使用多对多表

PHP
慕的地10843 2021-05-09 16:34:35
我需要进行类似测验的开发。我有3个表: quiz,quiz_questions,quiz_questions_answers。我以表格形式呈现这样的问题/答案。<?php foreach ($modelQuestions as $modelQuestion): ?>            <?= $modelQuestion->question ?> <br/>            <?= $form->field($modelMatch, 'answer[]')->textarea(['rows' => 6]) ?><?php endforeach; ?>在控制器中,我需要保存id_quiz并obs在表中quiz,但是我需要将其保存在quiz_questions_answers像这样的多对多表中id_quiz,id_question并且answer需要每个答案。我正在foreach循环中尝试此操作,但是如何获得每个答案的“ id_question”?public function actionCreate()    {        $model = new Quiz();        $modelMatch = new QuizQuestionsAnswers();        $modelQuestions = QuizQuestions::find()->all();        if ($model->load(Yii::$app->request->post()) && $modelMatch->load(Yii::$app->request->post())){            $model->save();            foreach ($modelMatch->answer as $answer) {                    $modelMatch = new QuizQuestionsAnswers();                    $modelMatch->id_quis = $model->id;                    $modelMatch->id_question = ????;                    $modelMatch->answer = $answer;                    $modelMatch->save();            }            return $this->redirect(['view', 'id' => $model->id]);        } else {            return $this->render('create', [                'model' => $model,                'modelMatch' => $modelMatch,                'modelQuestions' => $modelQuestions,            ]);        }    }这是一个方案创建,并且在我需要为方案更新做好准备之后。我迷路了..模式数据库为:
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 155 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信