如何创建一个超级菜单?



我正在用Go写一个电子商务应用程序,我已经在大菜单上呆了一段时间了,试图用PostgreSQL表实现它。最终结果,JSON响应,看起来像这样:

[
{ 
"ID": 5000,
"Name": "Department 1",
"Slug": "",
"CategoryGroups": [
{
"ID": 1000,
"Name": "Category Group 1",
"Slug": "",
"Categories": [
{
"ID": 1,
"Name": "Category A",
"Slug": "category-a"
},
{
"ID": 2,
"Name": "Category B",
"Slug": "category-b"
},
},
{
"ID": 1001,
"Name": "Category Group 2",
"Slug": "category-group-2",
},
},
{ 
"ID": 5001,
"Name": "Department 2",
"Slug": "department-2",
}
]

我试了如下(SQLFiddle):

表:

type Course struct {
gorm.Model
CourseName string `json:"course_name"`
CourseSlug string `json:"course_slug"`
}
type Subject struct {
gorm.Model
SubjectName string `json:"subject_name"`
SubjectSlug string `json:"subject_slug"`
CourseID    int    `json:"course_id"`
}
type Chapter struct {
gorm.Model
ChapterName string `json:"chapter_name"`
ChapterSlug string `json:"chaper_slug"`
SubjectID   int    `json:"subject_id"`
}
type Subchapter struct {
gorm.Model
SubchapterName string `json:"subchapter_name"`
SubchapterSlug string `json:"subchapter_slug"`
ChapterID      int    `json:"chapter_id"`
}
type CourseSubjChapterSub struct {
CourseName     string `json:"course_name"`
CourseSlug     string `json:"course_slug"`
SubjectName    string `json:"subject_name"`
SubjectSlug    string `json:"subject_slug"`
ChapterName    string `json:"chapter_name"`
ChapterSlug    string `json:"chaper_slug"`
SubchapterName string `json:"subchapter_name"`
SubchapterSlug string `json:"subchapter_slug"`
}
查询:

var res []models.CourseSubjChapterSub
db.DBClient.Model(&models.Course{}).
Select("courses.course_name, courses.course_slug, subjects.subject_name, subjects.subject_slug, chapters.chapter_name, chapters.chapter_slug, subchapters.subchapter_name, subchapters.subchapter_slug").
Joins("LEFT JOIN subjects ON courses.id = subjects.course_id").
Joins("LEFT JOIN chapters ON subjects.id = chapters.subject_id").
Joins("LEFT JOIN subchapters ON chapters.id = subchapters.chapter_id").
Scan(&res)
log.Println(res)

结果(日志):

[
{Web Designing web-design HTML html HTML Form html-form  }
{Web Designing web-design HTML html HTML Image html-image  }
{Web Designing web-design HTML html HTML Link html-link HTML Image Link html-image-link}
{Web Designing web-design HTML html HTML Link html-link HTML Text Link html-text-link}
{Web Designing web-design HTML html HTML List html-list HTML Ordered List html-ordered-list}
{Web Designing web-design HTML html HTML List html-list HTML Unordered List html-unordered-list}
{Web Designing web-design HTML html HTML Text html-text HTML Heading html-heading}
{Web Designing web-design HTML html HTML Text html-text HTML Paragraph html-paragraph}
{Web Designing web-design CSS css CSS Border css-border CSS Border Color css-border-color}
{Web Designing web-design CSS css CSS Border css-border CSS Border Style css-border-style}
{Web Designing web-design CSS css CSS Border css-border CSS Border Width css-border-width}
{Web Designing web-design CSS css CSS Position css-position CSS Absolute Position css-absolute-position}
{Web Designing web-design CSS css CSS Selector css-selector CSS Element Selector css-element-selector}
{Web Designing web-design JavaScript javascript    }
{Web Development web-development PHP php PHP Arrays php-arrays PHP Associative Array php-associative-array}
{Web Development web-development PHP php PHP Arrays php-arrays PHP Index Array php-index-array}
{Web Development web-development PHP php PHP Conditions php-conditions PHP If Condition php-if-condition}
{Web Development web-development PHP php PHP Conditions php-conditions PHP Switch Condition php-switch-condition}
{Web Development web-development Python python    }
{Web Development web-development .NET dotnet    }
{Programming programming Java java Java Methods java-methods Java Method Overloading java-method-overloading}
{Programming programming Java java Java Methods java-methods Java Method Parameter java-method-parameter} {Programming programming C++ cpp    }
]

我还尝试使用一个表的类别和手动分配部门和类别组,然后通过JavaScript在客户端做一些逻辑来创建大菜单。这也可以,但不完全是我想要的…

任何帮助将是非常感激的,欢呼声

答案很简单,我只是在Gorm文档中错过了它。

  1. 像这样修改结构:
type Course struct {
...
Subjects []Subject
}
  1. 为多个课程更新res变量并查询
var res []models.Course
db.Preload("Subjects.Chapters.Subchapters").
Preload(clause.Associations).
Find(&res)

相关内容

  • 没有找到相关文章

最新更新