0

In my project I want to insert multiple rows of data at a single time using the foreach loop. I have a variable which has array of elements.

For instance if my array has say 3 different elements. I want to save all these 3 elements in the 3 different db table rows. I also have other columns which are same for all the 3 array elements.

I have put them inside foreach statement but only the 1st elements gets saved. Is there any method I can achieve this?

My code

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,
            ]);
        }
    }

Only the productline_id is different other columns will have same data for all the prdouctline_id.

Thank You!!!

4
  • what is the output of this line $productlineID = Productlines::find()->where(['area_id' => $model->productline_id, 'product_id' => $prodID])->all(); I think you are getting single record only. Commented Oct 19, 2016 at 9:32
  • No I am getting multiple records I have dumped it and checked Commented Oct 19, 2016 at 9:35
  • can you please show me your $productlineID output. Commented Oct 19, 2016 at 9:45
  • Got it resolved... I had hard-coded $productlineID for testing purpose it was taking in that value and saving it my bad..got it fixed now.. Thanks a million Commented Oct 19, 2016 at 10:07

3 Answers 3

4

You have only one model object, and you are saving only to it. Try this:

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,
        ]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I see this is not making any difference .. Tried it
0

maybe you can modify my code

   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,



           ]);
       }
   }

1 Comment

focus to createmultiple , you can modify date
0

You need to create a different object to save in different rows. For loop executes 3 times but every time same object is being updated. You can define new object and save each time. Below code will work

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,
            ]);
        }
    }

2 Comments

How it is different than Exisiting answer given by Imaginaroom
I didn't see that answer, Both are similar. Didn't that work?

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.