Insert multiple data into database in Yii 2

后端 未结 6 1229
死守一世寂寞
死守一世寂寞 2020-12-06 07:24

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 $         


        
6条回答
  •  时光取名叫无心
    2020-12-06 08:09

    I think batch insert is the best solution for this problem.

    but there is one problem with your pic of code that is this code will create only one data row(first row) in data table and update to that same model

    solution for your code is

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

提交回复
热议问题