public function getAnswers() { return $this->hasMany(Answer::className(), ['answer_list_id' => 'id']); }
/** * @return string * * Отображает чистую форму для заполнения со списком вопросов */ public function actionUpdate($id) { $modelAnswerList = $this->findAnswerListModel($id); $modelQuestionList = $modelAnswerList->questionList; $modelsQuestion = $modelQuestionList->questions; $modelsAnswer = $modelAnswerList->answers; if (!$this->getAccessToOffice($modelAnswerList->do_id)) { Yii::$app->getResponse()->redirect(Url::toRoute(['write-test/index'])); } // если форма отправлена. if ($postData = Yii::$app->request->post()) { $valid = $modelQuestionList->validate(); if (isset($postData['Answer'])) { $answerIds = ArrayHelper::getColumn($postData['Answer'], 'id'); $modelsAnswer = Answer::findAll($answerIds); Model::loadMultiple($modelsAnswer, $postData); } foreach ($modelsAnswer as $indexModelAnswer => $modelAnswer) { $modelAnswer->profile_id = Yii::$app->user->identity->username; $valid = $modelAnswer->validate() && $valid; } if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { $flag = false; if ($modelAnswerList->save()) { foreach ($modelsAnswer as $indexModelAnswer => $modelAnswer) { if (!($flag = $modelAnswer->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $modelAnswerList->id]); } else { $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); } } } return $this->render('update', ['modelQuestionList' => $modelQuestionList, 'modelAnswerList' => $modelAnswerList, 'modelsQuestion' => $modelsQuestion, 'modelsAnswer' => $modelsAnswer]); }
<?php use yii\helpers\Html; use yii\grid\GridView; use yii\data\ActiveDataProvider; use app\modules\unicred\models\Answer; /* @var $this yii\web\View */ /* @var $modelAnswerList app\modules\unicred\models\AnswerList */ $this->title = "Ответы на опрос '{$model->list_name}'"; ?> <div class="answer-list-view"> <h1><?php echo Html::encode($this->title); ?> </h1> <?php echo GridView::widget(['dataProvider' => new ActiveDataProvider(['query' => Answer::find()->where(['answer_list_id' => $model->id])]), 'columns' => ['question_text', 'question_type', 'answer']]); ?> </div>