Yii2使用actionCreate中的foreach循环在数据库中保存多个数据

时间:2016-10-19 08:36:56

标签: arrays database foreach yii2 controllers

在我的项目中,我想使用foreach循环一次插入多行数据。我有一个包含元素数组的变量。

例如,如果我的数组有3个不同的元素。我想在3个不同的db表行中保存所有这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的相同数据。

谢谢!!!

3 个答案:

答案 0 :(得分: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,
        ]);
    }
}

答案 1 :(得分: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,



           ]);
       }
   }

答案 2 :(得分: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,
            ]);
        }
    }