diff --git a/services/category/CategoryMysqldb.php b/services/category/CategoryMysqldb.php index 88d08ccbd94c829378bbf3530ab12c6f01d8cb63..cd74a278203b8834bde3ea5724f6112d72bf4471 100644 --- a/services/category/CategoryMysqldb.php +++ b/services/category/CategoryMysqldb.php @@ -26,6 +26,10 @@ class CategoryMysqldb extends Service implements CategoryInterface protected $_categoryModel; + protected $_categoryProductModelName = '\fecshop\models\mysqldb\CategoryProduct'; + + protected $_categoryProductModel; + protected $serializeAttrs = [ 'name', 'menu_custom', @@ -41,6 +45,8 @@ class CategoryMysqldb extends Service implements CategoryInterface { parent::init(); list($this->_categoryModelName, $this->_categoryModel) = Yii::mapGet($this->_categoryModelName); + list($this->_categoryProductModelName, $this->_categoryProductModel) = \Yii::mapGet($this->_categoryProductModelName); + } // 保存的数据进行serialize序列化 @@ -298,11 +304,14 @@ class CategoryMysqldb extends Service implements CategoryInterface Yii::$service->url->removeRewriteUrlKey($url_key); $model->delete(); $this->removeChildCate($id); + // delete category product relation + $this->removeCategoryProductRelationByCategoryId($id); } else { Yii::$service->helper->errors->add("Category Remove Errors:ID:{id} is not exist.", ['id' => $id]); $deleteAll = false; } + } return $deleteAll; } else { @@ -319,6 +328,8 @@ class CategoryMysqldb extends Service implements CategoryInterface return false; } + // delete category product relation + $this->removeCategoryProductRelationByCategoryId($id); } return true; } @@ -335,6 +346,8 @@ class CategoryMysqldb extends Service implements CategoryInterface $url_key = $one['url_key']; Yii::$service->url->removeRewriteUrlKey($url_key); $one->delete(); + // delete category product relation + $this->removeCategoryProductRelationByCategoryId($idVal); } } } @@ -592,4 +605,10 @@ class CategoryMysqldb extends Service implements CategoryInterface return $arr; } + + public function removeCategoryProductRelationByCategoryId($category_id) + { + return $this->_categoryProductModel->deleteAll(['category_id' => $category_id]); + } + } diff --git a/services/product/ProductMysqldb.php b/services/product/ProductMysqldb.php index d32e9cc0eec518058372904a6200f97e6818652c..376d2c8cd6c9e40de376af895195460737e7f16a 100644 --- a/services/product/ProductMysqldb.php +++ b/services/product/ProductMysqldb.php @@ -793,6 +793,7 @@ class ProductMysqldb extends Service implements ProductInterface Yii::$service->helper->errors->add('Product Remove Errors:ID:{id} is not exist', ['id'=>$id]); $removeAll = 0; } + $this->removeCategoryProductRelationByProductId($id); } if (!$removeAll) { return false; @@ -808,6 +809,7 @@ class ProductMysqldb extends Service implements ProductInterface Yii::$service->search->removeByProductId($model[$this->getPrimaryKey()]); Yii::$service->product->stock->removeProductStock($id); $model->delete(); + $this->removeCategoryProductRelationByProductId($id); //$this->removeChildCate($id); } else { Yii::$service->helper->errors->add('Product Remove Errors:ID:{id} is not exist.', ['id'=>$id]); @@ -1148,4 +1150,10 @@ class ProductMysqldb extends Service implements ProductInterface public function updateAllScoreToZero(){ return $this->_productModel->getCollection()->update([], ['score' => 0]); } + + + public function removeCategoryProductRelationByProductId($product_id) + { + return $this->_categoryProductModel->deleteAll(['product_id' => $product_id]); + } }