简体   繁体   English

如何去/ Gorm一个megamenu?

[英]How to Go/Gorm a megamenu?

I'm writing an e-commerce app in Go, and I've been stuck on the megamenu for a while now, trying to implement it with PostgreSQL tables.我在Go写了一个电商app,卡在megamenu有一段时间了,想用PostgreSQL表来实现。 The end result, JSON response, would look something like this:最终结果 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",
}
]

I tried the following( SQLFiddle ):我尝试了以下( SQLFiddle ):

Tables:表:

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"`
}

Query:询问:

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)

Result(log):结果(日志):

[
{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    }
]

I also tried using one table for categories and manually assigning department and category-group, and then doing some logic on the client-side via JavaScript to create the megamenu.. that also works, but not quite what I'm looking for...我还尝试使用一个表作为类别并手动分配部门和类别组,然后通过 JavaScript 在客户端执行一些逻辑来创建 megamenu ..这也有效,但不是我正在寻找的.. .

Any help would be much appreciated, Cheers任何帮助将不胜感激,干杯

The answer is simple, I just missed it in the Gorm docs.答案很简单,我只是在 Gorm 文档中遗漏了它。

  1. Modify structs like so:像这样修改结构:
type Course struct {
  ...
  Subjects []Subject
}
  1. Update res variable for multiple Courses and query更新多个课程的res变量并查询
var res []models.Course
db.Preload("Subjects.Chapters.Subchapters").
  Preload(clause.Associations).
  Find(&res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM