0

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

查看代码:

<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,
        ]);
    }
}
4

1 回答 1

0

要访问表格输入,您应该使用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

于 2016-05-02T09:06:12.953 回答