0

In my project I want to save multiple data in seperate database table rows.

In my Controller action the code is below:

$deletedProducts = new \app\models\ProductlinesDeleted();
$app = \app\models\Productlines::find()
                                ->where(['product_id' => $id])->all();

        foreach ($app as $prod){
           $deletedProducts->productline_id = $prod->productline_id;
           $deletedProducts->area_id = $prod->area_id;
           $deletedProducts->product_id = $id;
           $deletedProducts->internal_code = $prod->internal_code;
           $deletedProducts->save();
           $prod->delete();
        }
          return $this->redirect(['index']);

The $app can return multiple products. SO based on that i want to save the data in the database table.

  • Every product has an area_id.
  • Every product will have a single area_id.

For example i have

  • Product 1 with area 1
  • Product 1 with area 2

So there are 2 products with different areas. When I perform the action, It has to save:

  • Product 1 with area 1 in the database table row.
  • Product 1 with area 2 in the database table next row.

    How can i achieve this?? Thank you..

2 Answers 2

1

Try This :

$app = \app\models\Productlines::find()
                                ->where(['product_id' => $id])->all();
    foreach ($app as $prod){
       $deletedProducts = new \app\models\ProductlinesDeleted();
       $deletedProducts->productline_id = $prod->productline_id;
       $deletedProducts->area_id = $prod->area_id;
       $deletedProducts->product_id = $id;
       $deletedProducts->internal_code = $prod->internal_code;
       $deletedProducts->save();
       $prod->delete();
    }
      return $this->redirect(['index']);
Sign up to request clarification or add additional context in comments.

1 Comment

you created $deletedProducts varible above the loop so every the existing record over written , and i put $deletedProducts in loop. so every time new object will be created and new record saved
0

You can use the beforeDelete event inside your active record model file like

public function beforeDelete() {
  $deletedProducts = new \app\models\ProductlinesDeleted();
  $deletedProducts->productline_id = $this->productline_id;
  $deletedProducts->area_id = $this->area_id;
  $deletedProducts->product_id = $this->product_id;
  $deletedProducts->internal_code = $this->internal_code;
  $deletedProducts->save();

  return parent::beforeDelete();
}

This method will get call for each record of your table row. So for delete the product your code will be like

$products = \app\models\Productlines::find()->where(['product_id' => $id])->all();
foreach ($products as $prod){
   $prod->delete(); //beforeDelete event method will get call while you call this delete method.
}
return $this->redirect(['index']);

Note: beforeDelete event method will get call only if you are deleting a record by using above delete method.

2 Comments

Actually this is a part of my actionDelete(). I am passing area_id from my searchModel which i cannot pass that to beforeDelete() method. Based on searchModel condition i delete the data. Anyways Thank you for your suggestion
@MohanPrasad Actually finding the products and foreach loop will be inside your actionDelete() and beforeDelete will be inside the Productlines model file. For your reference - yiiframework.com/doc-2.0/…. Anyway your problem get solved either way. all the best.

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.