<legend id='Mavya'><style id='Mavya'><dir id='Mavya'><q id='Mavya'></q></dir></style></legend>
    • <bdo id='Mavya'></bdo><ul id='Mavya'></ul>
    <i id='Mavya'><tr id='Mavya'><dt id='Mavya'><q id='Mavya'><span id='Mavya'><b id='Mavya'><form id='Mavya'><ins id='Mavya'></ins><ul id='Mavya'></ul><sub id='Mavya'></sub></form><legend id='Mavya'></legend><bdo id='Mavya'><pre id='Mavya'><center id='Mavya'></center></pre></bdo></b><th id='Mavya'></th></span></q></dt></tr></i><div id='Mavya'><tfoot id='Mavya'></tfoot><dl id='Mavya'><fieldset id='Mavya'></fieldset></dl></div>

    <small id='Mavya'></small><noframes id='Mavya'>

  • <tfoot id='Mavya'></tfoot>

        在 Yii 2 中将多个数据插入数据库

        时间:2023-10-16

          <legend id='gaVa5'><style id='gaVa5'><dir id='gaVa5'><q id='gaVa5'></q></dir></style></legend>

            <i id='gaVa5'><tr id='gaVa5'><dt id='gaVa5'><q id='gaVa5'><span id='gaVa5'><b id='gaVa5'><form id='gaVa5'><ins id='gaVa5'></ins><ul id='gaVa5'></ul><sub id='gaVa5'></sub></form><legend id='gaVa5'></legend><bdo id='gaVa5'><pre id='gaVa5'><center id='gaVa5'></center></pre></bdo></b><th id='gaVa5'></th></span></q></dt></tr></i><div id='gaVa5'><tfoot id='gaVa5'></tfoot><dl id='gaVa5'><fieldset id='gaVa5'></fieldset></dl></div>
              <tbody id='gaVa5'></tbody>

            <small id='gaVa5'></small><noframes id='gaVa5'>

              • <bdo id='gaVa5'></bdo><ul id='gaVa5'></ul>
                  <tfoot id='gaVa5'></tfoot>
                  本文介绍了在 Yii 2 中将多个数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当我尝试将多个数据同时保存到数据库时,我的代码有问题,这是我保存到数据库中的代码:

                  I have problem with my code when i'm trying to save multiple data into database at the same time, this is my code to save into database:

                  foreach ($data as $value) {
                     $model->route = $value[0][1];
                     $model->begin_point = $value[0][2];
                     $model->begin_point = $value[0][3];
                     $model->save();
                  }
                  return $this->redirect('index');
                  

                  每次我试图保存时,我只得到最后一个数据数组可以保存到数据库中.有人可以帮我吗?或者如果有人可以提供教程,那将是真正的帮助.

                  every i'm trying to save, i'm only get the last data array can save into database. could someone help me? or if someone could provide a tutorial, that would be a real help.

                  推荐答案

                  1. 通过循环多个值来创建一个数组.

                  1. Create an array by looping your multiple values.

                  $data- has multiple values
                  $bulkInsertArray = array();
                  foreach($data as $value){
                     $bulkInsertArray[]=[
                         'columnName1'=>$value[0][1],
                         'columnName2'=>$value[0][2],
                         'columnName3'=>$value[0][3]
                     ];
                  }
                  

                • 检查 $bulkInsertArray 是否为空

                • Check $bulkInsertArray in not empty

                  if(count($bulkInsertArray)>0){
                      $columnNameArray=['columnName1','columnName2','columnName3'];
                      // below line insert all your record and return number of rows inserted
                      $insertCount = Yii::$app->db->createCommand()
                                     ->batchInsert(
                                           $tableName, $columnNameArray, $bulkInsertArray
                                       )
                                     ->execute();
                  }
                  

                • 希望这些代码可能会有所帮助.

                  Hope these code may be helpful.

                  这篇关于在 Yii 2 中将多个数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Yii2 前后端链接(高级模板) 下一篇:如何仅在 yii2 中选择后才呈现视图

                  相关文章

                • <legend id='odNZJ'><style id='odNZJ'><dir id='odNZJ'><q id='odNZJ'></q></dir></style></legend>
                    <bdo id='odNZJ'></bdo><ul id='odNZJ'></ul>

                  <tfoot id='odNZJ'></tfoot>

                      <small id='odNZJ'></small><noframes id='odNZJ'>

                      <i id='odNZJ'><tr id='odNZJ'><dt id='odNZJ'><q id='odNZJ'><span id='odNZJ'><b id='odNZJ'><form id='odNZJ'><ins id='odNZJ'></ins><ul id='odNZJ'></ul><sub id='odNZJ'></sub></form><legend id='odNZJ'></legend><bdo id='odNZJ'><pre id='odNZJ'><center id='odNZJ'></center></pre></bdo></b><th id='odNZJ'></th></span></q></dt></tr></i><div id='odNZJ'><tfoot id='odNZJ'></tfoot><dl id='odNZJ'><fieldset id='odNZJ'></fieldset></dl></div>