Category.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\store\model\article;
  3. use think\Cache;
  4. use app\store\model\Article as ArticleModel;
  5. use app\common\model\article\Category as CategoryModel;
  6. /**
  7. * 文章分类模型
  8. * Class Category
  9. * @package app\store\model\article
  10. */
  11. class Category extends CategoryModel
  12. {
  13. /**
  14. * 分类详情
  15. * @param $category_id
  16. * @return Category|null
  17. * @throws \think\exception\DbException
  18. */
  19. public static function detail($category_id)
  20. {
  21. return static::get($category_id);
  22. }
  23. /**
  24. * 添加新记录
  25. * @param $data
  26. * @return false|int
  27. */
  28. public function add($data)
  29. {
  30. $data['wxapp_id'] = self::$wxapp_id;
  31. $this->deleteCache();
  32. return $this->allowField(true)->save($data);
  33. }
  34. /**
  35. * 编辑记录
  36. * @param $data
  37. * @return bool|int
  38. */
  39. public function edit($data)
  40. {
  41. $this->deleteCache();
  42. return $this->allowField(true)->save($data);
  43. }
  44. /**
  45. * 删除商品分类
  46. * @param $category_id
  47. * @return bool|int
  48. */
  49. public function remove($category_id)
  50. {
  51. // 判断是否存在文章
  52. $articleCount = ArticleModel::getArticleTotal(['category_id' => $category_id]);
  53. if ($articleCount > 0) {
  54. $this->error = '该分类下存在' . $articleCount . '个文章,不允许删除';
  55. return false;
  56. }
  57. $this->deleteCache();
  58. return $this->delete();
  59. }
  60. /**
  61. * 删除缓存
  62. * @return bool
  63. */
  64. private function deleteCache()
  65. {
  66. return Cache::rm('article_category_' . self::$wxapp_id);
  67. }
  68. }