在Foreach中循环不保存所有数据

时间:2019-01-08 06:49:03

标签: php foreach

我创建了一个foreach循环,其中包含带有产品信息的数据-id,价格,团体价格...我可以使用$_POST上的var_dump访问数据并查看数据。但是我无法将所有数据保存在数据库中。每次只保存上一个产品的信息。如何将所有产品保存在这两个表stackproduct中?这是我的代码:

 $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);
                }

这是我要保存的$_POST

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" } }

2 个答案:

答案 0 :(得分:1)

您在这里回答:

  $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);
                }

答案 1 :(得分:0)

我认为您的第一个问题是问题。 postedStacksArray它仅存储post值中的最后一个信息。一次打印postedStacksArray并检查