2016-10-19 46 views
0

在我的项目中,我希望在一次使用foreach循环插入多行数据。我有一个具有元素数组的变量。Yii2使用foreach循环保存数据库中的多个数据创建

例如,如果我的数组有说3个不同的元素。我想将所有这3个元素保存在3个不同的数据库表格行中。我也有其他所有3个数组元素相同的列。

我已经把它们放在foreach语句中,但只有第一个元素被保存。有什么方法可以实现吗?

我的代码

public function actionCreate($prodID) 
    { 
     $model = new ProductlinesStorage(); 

     if ($model->load(Yii::$app->request->post())) { 
      $productlineID = Productlines::find()->where(['area_id' => $model->productline_id, 'product_id' => $prodID])->all(); 

      foreach ($productlineID as $singleProductlineID) { 
       $model->productline_id = $singleProductlineID->productline_id; 
       $model->user_id = Yii::$app->user->identity->user_id; 
       $model->isNewRecord = true; 
       $model->save(); 
      } 
      return $this->redirect(['/product/storage?id='.$prodID]); 
     } else { 
      return $this->renderAjax('create', [ 
       'model' => $model, 
       'prodID' => $prodID, 
      ]); 
     } 
    } 

只有productline_id是不同的其他列将有相同的数据,所有的prdouctline_id。

谢谢!!!

+0

这是什么线'$ productlineID =输出代理产品::发现() - >其中([ 'AREA_ID'=> $模型 - > productline_id, 'PRODUCT_ID'=> $ PRODID ]) - > all();'我认为你只能获得单个记录。 –

+0

不,我得到多个记录,我已经倾倒它,并检查 –

+0

你能告诉我你的'$ productlineID'输出。 –

回答

2

你只有一个模型对象,而你只保存它。 试试这个:

public function actionCreate($prodID) 
{ 
    $model = new ProductlinesStorage(); 

    if ($model->load(Yii::$app->request->post())) { 
     $productlineID = Productlines::find()->where(['area_id' => $model->productline_id, 'product_id' => $prodID])->all(); 

     foreach ($productlineID as $singleProductlineID) { 
      $model = new ProductlinesStorage(); 
      $model->productline_id = $singleProductlineID->productline_id; 
      $model->user_id = Yii::$app->user->identity->user_id; 
      $model->isNewRecord = true; 
      $model->save(); 
     } 
     return $this->redirect(['/product/storage?id='.$prodID]); 
    } else { 
     return $this->renderAjax('create', [ 
      'model' => $model, 
      'prodID' => $prodID, 
     ]); 
    } 
} 
+0

我看到这是没有任何区别..试过了 –

0

也许你可以修改我的代码

public function actionCreate() 
 
    { 
 
     $model = new SemesterPendek(); 
 
     $model->user_id = \Yii::$app->user->identity->id; 
 
     $model->npm = \Yii::$app->user->identity->username; 
 

 

 
     $modelsNilai = [new Nilai]; 
 

 

 
     if ($model->load(Yii::$app->request->post())){ 
 
     $model->waktu_daftar = date('Y-m-d h:m:s'); 
 

 
     $model->save(); 
 

 
    $modelsNilai = Tabular::createMultiple(Nilai::classname()); 
 
      Tabular::loadMultiple($modelsNilai, Yii::$app->request->post()); 
 

 

 
      // validate all models 
 
      $valid = $model->validate(); 
 
      $valid = Tabular::validateMultiple($modelsNilai) && $valid; 
 

 
      if ($valid) { 
 
       $transaction = \Yii::$app->db->beginTransaction(); 
 
       try { 
 
        if ($flag = $model->save(false)) { 
 
         foreach ($modelsNilai as $indexTools =>$modelNilai) { 
 
          $modelNilai->id_sp = $model->id; 
 
         // $modelNilai->user_id = \Yii::$app->user->identity->id; 
 

 
          if (! ($flag = $modelNilai->save(false))) { 
 
           $transaction->rollBack(); 
 
           break; 
 
          } 
 
         } 
 
        } 
 
        if ($flag) { 
 
         $transaction->commit(); 
 
         return $this->redirect(['view', 'id' => $model->id]); 
 
        } 
 
       } catch (Exception $e) { 
 
        $transaction->rollBack(); \Yii::$app->session->setFlash('error','gagal'); 
 
       } 
 

 
     } 
 

 
     } else { 
 
      return $this->render('create', [ 
 
       'model' => $model, 
 
       'modelsNilai' => (empty($modelsNilai)) ? [new Nilai] : $modelsNilai, 
 

 

 

 
      ]); 
 
     } 
 
    }

+0

我不明白你的代码... –

+0

重点创建多个,你可以修改日期 –

0

您需要创建一个不同的对象不同行中保存。 For循环执行3次,但每次更新相同的对象。您可以定义新对象并每次保存。下面的代码将工作

public function actionCreate($prodID) 
    { 
     $model = new ProductlinesStorage(); 

     if ($model->load(Yii::$app->request->post())) { 
      $productlineID = Productlines::find()->where(['area_id' => $model->productline_id, 'product_id' => $prodID])->all(); 

      foreach ($productlineID as $singleProductlineID) { 
       $model = new ProductlinesStorage(); 
       $model->productline_id = $singleProductlineID->productline_id; 
       $model->user_id = Yii::$app->user->identity->user_id; 
       $model->isNewRecord = true; 
       $model->save(); 
      } 
      return $this->redirect(['/product/storage?id='.$prodID]); 
     } else { 
      return $this->renderAjax('create', [ 
       'model' => $model, 
       'prodID' => $prodID, 
      ]); 
     } 
    } 
+0

它是如何不同于由现在**给出的现存答案** –

+0

我没有看到答案,两者都是相似的。没有工作吗? –

+0

** Imaginaroom的**答案是正确的。 –

相关问题