表格数据输入无法正常工作。它只是在数据库中保存一条记录

时间:2019-07-17 07:00:17

标签: php yii2 yii2-basic-app

我正在使用加载倍数来保存表格数据输入,但是当我单击保存按钮时,只有一个记录被保存在数据库中,然后其余记录会自动填充与数据库中保存的记录相同的数据。在yii中保存多个相同的表单字段?

我已经在控制器中使用Model :: loadMultiple来保存一个模型的多个实例,并通过计算用户保存的模型实例的数量来使用foreach循环来保存数组模型,在我看来,我已经为每个实例使用了循环以将字段值保存在数组中。

我希望在所有字段都填满时通过一个保存按钮保存整个时间表,但是它只将一个记录保存到数据库中,而当我仅填充一个模型字段并单击“保存”时,所有字段都会自动填充。 / p>

控制器代码:

    public function actionView($id)
     {
    //Find out how many products have been submitted by the form
    $count = count(Yii::$app->request->post('Timetable', []));

    //Send at least one model to the form
    $modelTimetable = [new Timetable()];

    //Create an array of the products submitted
    for($i = 1; $i < $count; $i++) {
        $modelTimetable[] = new Timetable();
    }

    //Load and validate the multiple models
    if (Model::loadMultiple($modelTimetable, Yii::$app->request->post()) && Model::validateMultiple($modelTimetable)) {

        foreach ($modelTimetable as $modeltt) {

            //Try to save the models. Validation is not needed as it's 
    already been done.
            $modeltt->save(false);

        }

    }

     return $this->render('view', ['modelTimetable' => 
    $modelTimetable,'model' => $this->findModel($id)]);
}

查看代码:

    <div class="Timetableform">

     <? @var $modelTimetable app\models\Timetable; ?>      

    </div><!-- Timetableform -->

     <?php $form = ActiveForm::begin(); ?>


    <?php
    $query1=new \yii\db\Query(); 
    $query2=new \yii\db\Query(); 
    $row1 = $query1->select(["CONCAT(classes.Start_Time, '-', 
    classes.End_Time) AS classunit"])->from('class_units AS classes')- 
    >all();
    $row2=$query2->select(['Day_name'])->from('days')->all();
    $counter = 0;

      echo '<div class="col-xs-9">
     <div class="table-responsive">
        <table class="table table-striped" id="tt_data">
            <tr>
                <td class="blank"></td>';
            foreach ($row1 as $rowunits) {
                if(!empty($rowunits)){
                   echo '<td class="title">   
    '.$rowunits['classunit'].'<br></td>';
               }

                }
            echo '</tr>';

                foreach ($row2 as $rowdays) {   
                echo '<tr>';
                echo '<td class="time">'.$rowdays['Day_name'].'</td>';


    for ($i= 0; $i < 7; $i++ )
    {
       foreach ($modelTimetable as $index => $modeltt) {

     echo '<td class="drop">'.$form->field($modeltt, "[$counter]TAC_id")- 
   >dropDownList( ArrayHelper::map(teacherassignedcourses::find()- 
   >joinWith(['teachers'])->joinWith(['courses'])- 
   >all(),'TAC_id','courses.Course_title', 'teachers.Teacher_Name'), 
   ['prompt'=>'Teacher'])->label(false) ?>  
     <?= $form->field($modeltt, "[$counter]Room_id")- 
   >dropDownList(ArrayHelper::map(rooms::find()- 
   >all(),'Room_id','Room_Number'),['prompt'=>'Room'])->label(false) ?>
    <?= $form->field($modeltt, "[$counter]Class_type") ->textInput()- 
   >label(false) ?>
    <?= $form->field($modeltt, "[$counter]Shift") ->textInput()- 
   >label(false) ?>
    <?= $form->field($modeltt, "[$counter]Batch_id") ->textInput()- 
   >label(false) ?>
    <?= $form->field($modeltt, "[$counter]Day_id")->textInput()- 
    >label(false) ?>
    <?= $form->field($modeltt, "[$counter]Unit_id")->textInput()- 
   >label(false).
    '</td>';

    }
    $counter++;
    }


                     echo '</tr>';
                 }
       echo '</table>
    </div>
      </div>';
    ?>



    <div class="form-group">
    <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>




     <script type="text/javascript">
     var nodes = document.querySelectorAll("td.title");
     for(var i = 0; i < nodes.length; i++) {
     nodes[i].setAttribute("id", i+'a');
     }
     var nodes1 = document.querySelectorAll("td.time");
     for(var i = 0; i < nodes1.length; i++) {
     nodes1[i].setAttribute("id", i+'b');
    }
    </script>



    <?php ActiveForm::end(); ?>

1 个答案:

答案 0 :(得分:0)

SELECT [FIELD1],
    [FIELD2],
    [FIELD3],
    ....

说明

SELECT MAX(LEN([FIELD1])) AS [FIELD1],
    MAX(LEN([FIELD2])) AS [FIELD2],
    MAX(LEN([FIELD3])) AS [FIELD3],
    ....

它也可以如下使用:

public function actionView($id)
     {
    //Find out how many products have been submitted by the form
    $count = Yii::$app->request->post('form-name');  // form name in model class

    //Send at least one model to the form
    $modelTimetable = [new Timetable()];

    foreach($count as $key => $value) {
        $modelTimetable[$key] = new Timetable();
        $modelTimetable[$key]->attributes = $value;
     }

    //Load and validate the multiple models
    if (Model::loadMultiple($modelTimetable , Yii::$app->request->post(), 'form-name') &&  Model::validateMultiple($modelTimetable)) { 

        foreach ($modelTimetable as $modeltt) {

            //Try to save the models. Validation is not needed as it's 
    already been done.
            $modeltt->save(false);

        }

    }

     return $this->render('view', ['modelTimetable' => 
    $modelTimetable,'model' => $this->findModel($id)]);
}
相关问题