// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\model\article; use app\api\model\Article as ArticleModel; use app\common\model\article\Category as CategoryModel; /** * 文章分类模型 * Class Category * @package app\api\model\article */ class Category extends CategoryModel { /** * 隐藏字段 * @var array */ protected $hidden = [ 'is_delete', 'store_id', 'create_time', 'update_time' ]; /** * 获取分类列表 * @return \think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getShowList(): \think\Collection { return $this->getList(['status' => 1]); } public function add(array $data): bool { // 保存记录 $data['store_id'] = self::$storeId; return $this->save($data); } /** * 编辑记录 * @param array $data * @return bool */ public function edit(array $data): bool { // 保存记录 return $this->save($data); } /** * 删除商品分类 * @param int $categoryId * @return bool */ public function remove(int $categoryId): bool { // 判断是否存在文章 $articleCount = ArticleModel::getArticleTotal(['category_id' => $categoryId]); if ($articleCount > 0) { $this->error = '该分类下存在' . $articleCount . '个文章,不允许删除'; return false; } // 删除记录 return $this->delete(); } }