public function saveTags($posted_tags, $product_id)
 {
     $tags = explode(",", $posted_tags);
     if (!$tags) {
         return;
     }
     ProductTagProducts::deleteAll(["product_id" => $product_id]);
     foreach ($tags as $tagname) {
         $tagname = trim($tagname);
         $sql = "SELECT id FROM product_tags WHERE tag_name = :tag_name";
         $result = Yii::$app->db->createCommand($sql)->bindValue(":tag_name", $tagname, \PDO::PARAM_STR)->queryOne();
         if ($result) {
             $id = $result["id"];
         } else {
             $model = new ProductTags();
             $model->tag_name = $tagname;
             $model->save();
             $id = $model->id;
         }
         $tagproductModel = new ProductTagProducts();
         $tagproductModel->tag_id = $id;
         $tagproductModel->product_id = $product_id;
         $tagproductModel->save();
     }
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getTag()
 {
     return $this->hasOne(ProductTags::className(), ['id' => 'tag_id']);
 }
 /**
  * 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]);
     }
 }