Yii2-测验-使用多对多表

我需要进行类似测验的开发。我有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,

            ]);

        }

    }

这是一个方案创建,并且在我需要为方案更新做好准备之后。我迷路了..


模式数据库为:

http://img4.mukewang.com/60b04d380001036905110239.jpg

慕的地10843
浏览 156回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP