allen
allen

Reputation: 380

Loop in Foreach do not save all data

I create a foreach loop with data with product information - id, price, group price... I can access data and view data with var_dump on $_POST . But I can not save all data in database. Everytime it is save only info on last product. How can I save all products in this two table stack and product? Here's my code:

 $postedStacksArray = array();
                $postedStacksArray = array();
                $postedproductsArray = array();
                $postedStacksDetailsArray = 

                foreach ($_POST['Product']["rel_stacks"] as $keyStack => $rel_stack){
                    if(isset($rel_stack[0])){
                        $postedStacksArray['product_id'] = $rel_stack;
                        $postedproductsArray[] = (int)$rel_stack[
                        $postedStacksDetailsArray[$keyStack]['product_id'] = (int)$rel_stack[0];
                        $postedStacksDetailsArray[$keyStack]['price'] = $rel_stack['price'];
                        $postedStacksDetailsArray[$keyStack]['price_gr2'] = $rel_stack['price-gr2'];
                        $postedStacksDetailsArray[$keyStack]['price_gr3'] = $rel_stack['price-gr3'];
                    }
                }

                $savedStackArray = ArrayHelper::map(ProductStack::find()->where('stack_id = :stack_id and product_id=:product_id', ['stack_id' => $model->attribute_set_id, 'product_id' => $model->id])->all(), 'stack_id', 'stack_id');

                $stackForSaveArr = array_diff($postedStacksArray, $savedStackArray);
                $stackForDeleteArr = array_diff($savedStackArray, $postedStacksArray);


                foreach ($stackForSaveArr as $keyStack => $stak) {
                    $stack = new Stack;
                    $stack->product_id = $postedStacksArray['product_id'];
                    if(isset($stak['product_id']) and !empty($stak['product_id'])) {
                        $desc_product = Product::findOne($stak['product_id']);
                        if ($desc_product) {
                            if (isset($postedStacksDetailsArray["price"]) and !empty($postedStacksDetailsArray["price"])) {
                                $stack->price_disc_gr1 = $desc_product->price_online - $postedStacksDetailsArray["price"];
                            }
                            if (isset($postedStacksDetailsArray["price_gr2"]) and !empty($postedStacksDetailsArray["price_gr2"])) {
                                $stack->price_disc_gr2 = $desc_product->price_online_gr2 - $postedStacksDetailsArray["price_gr2"];
                            }
                            if (isset($postedStacksDetailsArray["price_gr3"]) and !empty($postedStacksDetailsArray["price_gr3"])) {
                                $stack->price_disc_gr3 = $desc_product->price_online_gr3 - $postedStacksDetailsArray["price_gr3"];
                            }
                        }
                    }
                    $stack->price_gr1 = $postedStacksDetailsArray["price"];
                    $stack->price_gr2 = $postedStacksDetailsArray["price_gr2"];
                    $stack->price_gr3 = $postedStacksDetailsArray["price_gr3"];
                    $stack->user_id = Yii::$app->user->identity->id;
                    $stack->save(false);

                    $newPrd = new Product;
                    $newPrd->type = 2;
                    $newPrd->parent_id = $model->id;
                    $newPrd->price_online = $postedStacksDetailsArray["price"];
                    $newPrd->price_online_gr2 = $postedStacksDetailsArray["price_gr2"];
                    $newPrd->price_online_gr3 = $postedStacksDetailsArray["price_gr3"];
                    $newPrd->active = 1;
                    $newPrd->in_stack = 1;
                    $lastStack = Stack::find()->orderBy(['id' => SORT_DESC])->one();
                    $newPrd->attribute_set_id = $lastStack->id;
                    $newPrd->artic_number = $keyStack.$model->id;
                    $newPrd->save(false);
                }

here is my $_POST for save

array(3) { ["first"]=> array(4) { [0]=> string(3) "709" ["price"]=> string(1) "4" ["price-gr2"]=> string(1) "3" ["price-gr3"]=> string(1) "2" } 
["second"]=> array(4) { [0]=> string(3) "707" ["price"]=> string(2) "19" ["price-gr2"]=> string(2) "18" ["price-gr3"]=> string(2) "17" } 
["last"]=> array(4) { [0]=> string(4) "1251" ["price"]=> string(1) "7" ["price-gr2"]=> string(1) "6" ["price-gr3"]=> string(1) "5" } }

Upvotes: 0

Views: 117

Answers (2)

Jeni Vasileva
Jeni Vasileva

Reputation: 788

here is you answer:

  $postedStacksArray = array();
                $postedproductsArray = array();
                $postedStacksDetailsArray = array();



                foreach ($_POST['Product']["rel_stacks"] as $keyStack => $rel_stack){
                    if(isset($rel_stack[0])){
                        $postedStacksArray['product_id'] = $rel_stack;
                        $postedproductsArray[] = (int)$rel_stack[0];
                        $postedStacksDetailsArray[$keyStack]['product_id'] = (int)$rel_stack[0];
                        $postedStacksDetailsArray[$keyStack]['price'] = $rel_stack['price'];
                        $postedStacksDetailsArray[$keyStack]['price_gr2'] = $rel_stack['price-gr2'];
                        $postedStacksDetailsArray[$keyStack]['price_gr3'] = $rel_stack['price-gr3'];
                    }
                }
 $savedStackArray = ArrayHelper::map(ProductStack::find()->where('stack_id = :stack_id and product_id=:product_id', ['stack_id' => $model->attribute_set_id, 'product_id' => $model->id])->all(), 'stack_id', 'stack_id');

                $stackForSaveArr = array_diff($postedproductsArray, $savedStackArray);
                $stackForDeleteArr = array_diff($savedStackArray, $postedproductsArray);


                foreach ($postedStacksDetailsArray as $keyStack => $stak) {
                    $stack = new Stack;
                    $stack->product_id = $stak["product_id"];

                    if(isset($stak['product_id']) and !empty($stak['product_id'])) {
                        $desc_product = Product::findOne($stak['product_id']);
                        if ($desc_product) {
                            if (isset($postedStacksDetailsArray[$keyStack]["price"]) and !empty($postedStacksDetailsArray[$keyStack]["price"])) {
                                $stack->price_disc_gr1 = $desc_product->price_online - $postedStacksDetailsArray[$keyStack]["price"];
                            }
                            if (isset($postedStacksDetailsArray[$keyStack]["price_gr2"]) and !empty($postedStacksDetailsArray[$keyStack]["price_gr2"])) {
                                $stack->price_disc_gr2 = $desc_product->price_online_gr2 - $postedStacksDetailsArray[$keyStack]["price_gr2"];
                            }
                            if (isset($postedStacksDetailsArray["price_gr3"]) and !empty($postedStacksDetailsArray[$keyStack]["price_gr3"])) {
                                $stack->price_disc_gr3 = $desc_product->price_online_gr3 - $postedStacksDetailsArray[$keyStack]["price_gr3"];
                            }
                        }
                    }
                    $stack->price_gr1 = $postedStacksDetailsArray[$keyStack]["price"];
                    $stack->price_gr2 = $postedStacksDetailsArray[$keyStack]["price_gr2"];
                    $stack->price_gr3 = $postedStacksDetailsArray[$keyStack]["price_gr3"];
                    $stack->user_id = Yii::$app->user->identity->id;
                    //var_dump($stack->product_id);die;
                    $stack->save(false);

                    $newPrd = new Product;
                    $newPrd->type = 2;
                    $newPrd->parent_id = $model->id;

                    $newPrd->price_online = $postedStacksDetailsArray[$keyStack]["price"];
                    $newPrd->price_online_gr2 = $postedStacksDetailsArray[$keyStack]["price_gr2"];
                    $newPrd->price_online_gr3 = $postedStacksDetailsArray[$keyStack]["price_gr3"];
                    $newPrd->active = 1;
                    $newPrd->in_stack = 1;
                    $lastStack = Stack::find()->orderBy(['id' => SORT_DESC])->one();
                    $newPrd->attribute_set_id = $lastStack->id;
                    $newPrd->artic_number = $keyStack.$model->id;
                    $newPrd->save(false);
                }

Upvotes: 1

I think problem in your first foreach. postedStacksArray it is storing only last info frompost value. Print postedStacksArray once and check

Upvotes: 0

Related Questions