3

Hello i want to save multiple form field value into MySQL database table using Yii2 with out writing any custom query code. My table structure is where id primary key, auto_incremented. My array structure is given below

Form field is

   <tr>
              <td>1</td>
              <td><?= $form->field($coordinatemodel,'lat_degree[]')->textInput(['placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_min[]')->textInput(['placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_second[]')->textInput(['placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_degree[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_min[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_second[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
            </tr> 
            <tr>
              <td>2</td>
              <td><?= $form->field($coordinatemodel,'lat_degree[]')->textInput(['placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_min[]')->textInput(['placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_second[]')->textInput(['placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_degree[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_min[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_second[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
            </tr> 
            <tr>
              <td>3</td>
              <td><?= $form->field($coordinatemodel,'lat_degree[]')->textInput(['placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_min[]')->textInput(['placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_second[]')->textInput(['placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_degree[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_min[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_second[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
            </tr> 

id lat_degree lat_min lat_second long_degree long_min long_second
1  50         50      50          55          55       55
2  60         60      60          65          65       65
3  65         65      65          70          70       70

My Array Structure is like below

3 Answers 3

3

There is an explanation on this from the docs on tabular input. You can alter your actionCreate to include this.

First, you will need to create an array of models first to be populated:

$count = count(Yii::$app->request->post('CoordinateDetails', []));
$coordinateDetails = [new CoordinateDetail()];
for ($i = 1; $i < $count; $i++) {
    $coordinateDetails[] = new CoordinateDetail();
}

You can then use loadMultiple and validateMultiple to populate and validate your models. You will however have to loop through your models in order to save them:

if (CoordinateDetail::loadMultiple($coordinateDetails, Yii::$app->request->post()) 
    && CoordinateDetail::validateMultiple($coordinateDetails)) {
    foreach ($coordinateDetails as $coordinateDetail) {
        //do any other thing here e.g. setting foreign keys
        $coordinateDetail->save(false);
    }
    return $this->redirect('index');
}
else {
    // do something for the error
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you should use batchInsert Method:

        \Yii::$app->db->createCommand()->batchInsert('yourtable', ['lat_degree','lat_min','lat_second',...],$CoordinatesDetail )
        ->bindValue(':id', 1)
        ->execute();

Comments

0

When i have these kind of scenario in my project then i use yii2-dynamicform extension which states:

It is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.

Check it's demo here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.