public function saveToppings(array $posted_toppings, $product_id)
 {
     $this->deleteAll(["product_id" => $product_id]);
     foreach ($posted_toppings["name"] as $key => $row) {
         $model = new ProductToppings();
         $model->product_id = $product_id;
         $model->name = $row;
         $model->price = $posted_toppings["price"][$key];
         $model->save();
     }
 }
 /**
  * Updates an existing Products model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $imageModel = new ProductImages(['scenario' => 'update']);
     $images = $imageModel->getProductImage($id);
     $imageModel->image = $images["image"];
     $tagsModel = new ProductTags();
     $tagsModel->tag_name = $tagsModel->getTagNames($id);
     $toppingsModel = new ProductToppings();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         if (!is_dir($this->imagePath . DIRECTORY_SEPARATOR . $model->slug)) {
             mkdir($this->imagePath . DIRECTORY_SEPARATOR . $model->slug, 0755, true);
         }
         $imageModel->load(\Yii::$app->request->getBodyParams());
         $file = UploadedFile::getInstance($imageModel, 'image');
         if (!is_null($file)) {
             $fileDir = $this->imagePath . DIRECTORY_SEPARATOR . $model->slug . DIRECTORY_SEPARATOR;
             $file->saveAs($fileDir . $file->getBaseName() . "." . $file->getExtension());
             $save = Image::open($fileDir . $file->getBaseName() . "." . $file->getExtension())->zoomCrop(365, 365)->merge(Image::open($this->defaultImagePath . "/logo.png")->cropResize(100, 100), 10, 10)->save($fileDir . $file->getBaseName() . "_thumb." . $file->getExtension(), $file->getExtension(), 80);
             $imageModel->saveImages($model->slug . "/" . $file->getBaseName() . "." . $file->getExtension(), $model->slug . "/" . $file->getBaseName() . "_thumb." . $file->getExtension(), $model->id);
         }
         $tagsModel->saveTags($_POST["ProductTags"]["tag_name"], $model->id);
         $toppingsModel->saveToppings($_POST["ProductToppings"], $model->id);
         return $this->redirect(['index']);
     } else {
         return $this->render('update', ['categories' => ArrayHelper::map(ProductCategories::find()->orderBy('title')->all(), 'id', 'title'), 'model' => $model, 'imageModel' => $imageModel, 'tagsModel' => $tagsModel, 'toppingsModel' => $toppingsModel, 'toppings' => ProductToppings::find()->where(["product_id" => $id])->asArray()->all(), "images" => $images]);
     }
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getProductToppings()
 {
     return $this->hasMany(ProductToppings::className(), ['product_id' => 'id']);
 }
 public function actionAppendedit()
 {
     if (Yii::$app->request->getIsPost()) {
         $product = Products::find()->where(["id" => $_POST["product"]])->asArray()->one();
         $toppings = false;
         if (isset($_POST["topping"])) {
             $toppings = ProductToppings::find()->where(["id" => array_keys($_POST["topping"])])->asArray()->all();
         }
         return $this->renderAjax("renderprod", ["product" => $product, "toppings" => $toppings, "count" => $_POST["count"]]);
     }
 }