将表格的字段显示三次并唯一保存值


Displaying the fields of tables three times and saving the value uniquely

在保存显示三次的表的字段时,我遇到了一个问题。无法保存保存在文本字段中的唯一值。请有人告诉我正确的答案。

查看代码:

<h2>List of Documents</h2>
<table class="table">
    <?php foreach($formlist as $item) { ?>
    <tr>
        <td><?= $form->field($model, '['.$item->id.']value')->radioList(['yes'=>' yes','no'=>' no'])->label($item['title']); ?></td>
    </tr>
    <?php } ?>
</table>

控制器代码:

public function actionCreate()
{
    $model = new Form();
    $forminfo = new Forminfo();
    $forminfo->id = $model->forminfo_id;
    /*$sql = 'SELECT * FROM formlist ORDER BY id ASC';
    $db = Yii::$app->db;
    $formlist = $db->createCommand($sql)->queryAll();*/
    // same of ->
    $formlist = Formlist::find()->orderBy(['id'=>SORT_ASC])->all();
    if ($forminfo->load(Yii::$app->request->post()) && $model->load(Yii::$app->request->post())) {
         $forminfo->save(false); // skip validation as model is already validated
         $model->forminfo_id =  $forminfo->id; // no need for validation rule on user_id as you set it yourself
         $model->save(false); 
         Yii::$app->getSession()->setFlash('success', 'You have successfully saved your data.');
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
            'forminfo' => $forminfo,
            'formlist' => $formlist,
        ]);
    }
}

访问表格输入时,应使用loadMultiple('yourModel')

或上的环路

 $post= Yii::$app->request->post());
 foreach ($post['your_model'] as $key => $myModel) {
   //  $myModel contain the current model 
 }

这个yii2指南可能很有用http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html