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(); } }
public function actionView($categoryslug, $productslug) { $categoryData = ProductCategories::find()->where(["slug" => $categoryslug])->asArray()->one(); $productData = Products::find()->where(["slug" => $productslug])->with(["productToppings", "productImages"])->asArray()->one(); $tagData = ProductTagProducts::find()->where(["product_id" => $productData["id"]])->with(["tag"])->asArray()->all(); $orderProducts = new OrderProducts(); return $this->render("view", ["categoryData" => $categoryData, "productData" => $productData, "tagData" => $tagData, "topSelling" => $orderProducts->getTopSellingProducts(9)]); }
/** * Deletes an existing Products model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { ProductImages::deleteAll(["product_id" => $id]); ProductTagProducts::deleteAll(["product_id" => $id]); $this->findModel($id)->delete(); return $this->redirect(['index']); }
/** * @return \yii\db\ActiveQuery */ public function getProductTagProducts() { return $this->hasMany(ProductTagProducts::className(), ['product_id' => 'id']); }