当前位置: 首页 > news >正文

goframe 博客分类文章模型文档 主要解决关联

goframe 博客文章模型文档

模型结构 (BlogArticleInfoRes)

BlogArticleInfoRes 结构体代表系统中的一篇博客文章,包含完整的元数据和内容管理功能。

type BlogArticleInfoRes struct {Id          uint         `orm:"id,primary" json:"id"`           // 唯一标识符Title       string       `orm:"title" json:"title"`             // 文章标题Content     string       `orm:"content" json:"content"`         // 文章内容Summary     string       `orm:"summary" json:"summary"`         // 文章摘要CategoryId  int          `orm:"category_id" json:"categoryId"`  // 分类IDTags        string       `orm:"tags" json:"tags"`               // 文章标签Cover       string       `orm:"cover" json:"cover"`             // 封面图片URLViewCount   int          `orm:"view_count" json:"viewCount"`    // 浏览次数Status      int          `orm:"status" json:"status"`           // 文章状态CreatedAt   *gtime.Time  `orm:"created_at" json:"createdAt"`   // 创建时间UpdatedAt   *gtime.Time  `orm:"updated_at" json:"updatedAt"`   // 更新时间IsTop       int          `orm:"is_top" json:"isTop"`           // 是否置顶Author      string       `orm:"author" json:"author"`           // 文章作者Category    *LinkedCategory `orm:"with:id=category_id" json:"category"` // 关联分类
}

字段说明

字段名类型JSON标签ORM标签描述
Iduintjson:“id”orm:“id,primary”文章唯一标识符
Titlestringjson:“title”orm:“title”文章标题
Contentstringjson:“content”orm:“content”文章主要内容
Summarystringjson:“summary”orm:“summary”文章简短摘要
CategoryIdintjson:“categoryId”orm:“category_id”文章分类ID
Tagsstringjson:“tags”orm:“tags”文章标签(逗号分隔)
Coverstringjson:“cover”orm:“cover”文章封面图片URL
ViewCountintjson:“viewCount”orm:“view_count”文章浏览次数
Statusintjson:“status”orm:“status”文章状态
CreatedAt*gtime.Timejson:“createdAt”orm:“created_at”文章创建时间戳
UpdatedAt*gtime.Timejson:“updatedAt”orm:“updated_at”最后更新时间戳
IsTopintjson:“isTop”orm:“is_top”是否置顶文章
Authorstringjson:“author”orm:“author”文章作者名称

关联关系说明

该模型通过 Category 字段与分类模型建立一对一关系:

Category *LinkedCategory `orm:"with:id=category_id" json:"category"`

ORM关联标签解析

  • with: 指定关联条件
  • id=category_id 表示当前模型的 category_id 字段与分类模型的 id 字段关联
  • 查询时自动进行数据库表连接

查询示例

func (s *sBlogArticle) GetArticleWithCategory(ctx context.Context, articleId uint) (*model.BlogArticleInfoRes, error) {var articleInfo *model.BlogArticleInfoReserr := dao.BlogArticle.Ctx(ctx).WithAll().Where("id", articleId).Scan(&articleInfo)if err != nil {return nil, err}// articleInfo.Category 将自动包含关联的分类信息return articleInfo, nil
}

状态值说明

  • 0: 草稿
  • 1: 已发布
  • 2: 审核中
  • 3: 已拒绝

置顶值说明

  • 0: 普通文章
  • 1: 置顶文章

该模型结构为博客文章管理提供了强大的基础,支持分类、标签和元数据跟踪等功能。

分类模型结构 (CmsCategoryInfoRes)

分类模型用于管理博客文章的分类信息,支持多级分类和SEO优化。

type CmsCategoryInfoRes struct {gmeta.Meta      `orm:"table:cms_category"`Id              uint        `orm:"id,primary" json:"id"`           // IDName            string      `orm:"name" json:"name"`               // 名称Type            string      `orm:"type" json:"type"`               // 类型ParentId        uint        `orm:"parent_id" json:"parentId"`      // 父IDSort            int         `orm:"sort" json:"sort"`               // 排序Status          string      `orm:"status" json:"status"`           // 状态Alias           string      `orm:"alias" json:"alias"`             // 别名CreatedAt       *gtime.Time `orm:"created_at" json:"createdAt"`    // 创建时间UpdatedAt       *gtime.Time `orm:"updated_at" json:"updatedAt"`    // 更新时间
}

分类字段说明

字段名类型描述
Iduint分类唯一标识符
Namestring分类名称
Typestring分类类型
ParentIduint父分类ID,用于构建分类层级
Sortint排序权重
Statusstring分类状态
Aliasstring分类别名,用于URL优化
CreatedAt*gtime.Time创建时间
UpdatedAt*gtime.Time更新时间

分类特性

  1. 多级分类

    • 通过 ParentId 支持无限级分类
    • 可以构建复杂的分类层级结构
  2. URL优化

    • 支持别名设置,优化URL结构
    • 更友好的SEO支持
  3. 状态管理

    • 可设置分类状态
    • 支持分类的启用/禁用管理

查询示例

// 获取分类及其子分类
func (s *sCmsCategory) GetCategoryWithChildren(ctx context.Context, categoryId uint) (*model.CmsCategoryInfoRes, error) {var category *model.CmsCategoryInfoReserr := dao.CmsCategory.Ctx(ctx).Where("id", categoryId).Scan(&category)if err != nil {return nil, err}// 获取子分类children, err := dao.CmsCategory.Ctx(ctx).Where("parent_id", categoryId).Order("sort asc").All()if err != nil {return nil, err}// 处理子分类...return category, nil
}// 获取分类树
func (s *sCmsCategory) GetCategoryTree(ctx context.Context) ([]*model.CmsCategoryInfoRes, error) {// 获取所有分类categories, err := dao.CmsCategory.Ctx(ctx).Order("sort asc").All()if err != nil {return nil, err}// 构建分类树return buildCategoryTree(categories), nil
}

与文章模型的关联

文章模型通过 CategoryId 关联到分类模型:

Category *CmsCategoryInfoRes `orm:"with:id=category_id" json:"category"`

这种关联实现:

  • 文章分类的快速查询
  • 分类文章的统计
  • 分类导航的构建
  • 文章的多级分类展示

状态说明

  • 0: 禁用
  • 1: 启用
  • 2: 待审核

该分类模型为博客系统提供了灵活的文章分类管理功能,支持多级分类结构和基本的分类属性管理。

http://www.lryc.cn/news/527415.html

相关文章:

  • 【JavaWeb06】Tomcat基础入门:架构理解与基本配置指南
  • 安卓日常问题杂谈(一)
  • Kitchen Racks 2
  • 嵌入式学习笔记-杂七杂八
  • 14-7C++STL的stack容器
  • Vue 3 中的响应式系统:ref 与 reactive 的对比与应用
  • 物业巡更系统助推社区管理智能化与服务模式创新的研究与应用
  • windows蓝牙驱动开发-生成和发送蓝牙请求块 (BRB)
  • Linux网络之序列化和反序列化
  • linux设置mysql远程连接
  • react-native网络调试工具Reactotron保姆级教程
  • erase() 【删数函数】的使用
  • 性能测试丨内存火焰图 Flame Graphs
  • AIGC的企业级解决方案架构及成本效益分析
  • Linux 入门 常用指令 详细版
  • 【R语言】流程控制
  • 猿人学第一题 js混淆源码乱码
  • 计算机组成原理(2)王道学习笔记
  • 【AI日记】25.01.26
  • 三. Redis 基本指令(Redis 快速入门-03)
  • 设计模式的艺术-代理模式
  • C#新语法
  • 微信小程序压缩图片
  • 通义灵码插件保姆级教学-IDEA(安装及使用)
  • windows下本地部署安装hadoop+scala+spark-【不需要虚拟机】
  • 倍频增量式编码器--角度插值法输出A,B(Aangular Interpolation)
  • LSM对于特殊数据的优化手段
  • 83,【7】BUUCTF WEB [MRCTF2020]你传你[特殊字符]呢
  • Go语言入门指南(二): 数据类型
  • 2025.1.26机器学习笔记:C-RNN-GAN文献阅读