普通网友 2018-07-11 13:54
浏览 73
已采纳

如何在gorm中获取结构(内部结构)值

I'm newbie of Golang and GORM. I get some issues on it. How can I get the inside struct value? (Like a nested struct in golang), I tried but, i didn't get actual result.

I have three structs

Department Struct

type Department struct {
    gorm.Model
    DepartmentName string
    DeptCode       string
    Employee       Employee //Employee struct
}

Employee struct

type Employee struct {
    gorm.Model
    EmpId           string
    EmpName         string
    DepartmentID    uint //Department id
    EmployeeContact []EmployeeContact //Array of Employee contact
}

Employee Contact

type EmployeeContact struct {
    gorm.Model
    ContactType string
    ContacText  string
    EmployeeID  uint //Employee Id
}

Relationship

# Department is a Parent of Employee.

# Employee is a Parent of Employee contacts.

I used GORM (Joins)

var departmentStruct model.Department
var employeeStruct model.Employee

    db.Debug().Model(&departmentStruct).Joins("JOIN employees ON employees.department_id = departments.id").Joins("JOIN employee_contacts ON employee_contacts.employee_id = employees.id").Select("employees.id,departments.department_name,departments.dept_code,employees.emp_id,employees.emp_name,employee_contacts.contact_type").Scan(&employeeStruct)
    res1B, _ := json.Marshal(employeeStruct)
    fmt.Fprintln(w, string(res1B))

It will be return the output is

{

    "ID":1,
    "EmpId":"001",
    "EmpName":"samsung",
    "DepartmentID":0, 
    "EmployeeContact":{ //It will be return empty
        "ID":0,
        "CreatedAt":"0001-01-01T00:00:00Z",
        "UpdatedAt":"0001-01-01T00:00:00Z",
        "DeletedAt":null,
        "ContactType":"",
        "ContacText":"",
        "EmployeeID":0
    }

}

I need, When I pass Employee id it will be return like given below format

{

    "ID":1,
    "EmpId":"001",
    "EmpName":"samsung",
    "Department":{
        "ID":1,
        "CreatedAt":"0001-01-01T00:00:00Z",
        "UpdatedAt":"0001-01-01T00:00:00Z",
        "DeletedAt":null,
        "DepartmentName":"Software Analyst",
        "deptCode":"SA"
    },
    "EmployeeContact":[
        {
            "ID":1,
            "CreatedAt":"0001-01-01T00:00:00Z",
            "UpdatedAt":"0001-01-01T00:00:00Z",
            "DeletedAt":null,
            "ContactType":"Home",
            "ContacText":"1234567890",
            "EmployeeID":1
        },
        {
            "ID":2,
            "CreatedAt":"0001-01-01T00:00:00Z",
            "UpdatedAt":"0001-01-01T00:00:00Z",
            "DeletedAt":null,
            "ContactType":"Office",
            "ContacText":"0123456789",
            "EmployeeID":1
        }
    ]
}

Any one can teach me?. How can I achieve it. Thanks.

  • 写回答

1条回答 默认 最新

  • doufei9805 2018-07-11 16:54
    关注

    First of all you probably want your employee model to like this

    type Employee struct {
        gorm.Model
        EmpId           string
        EmpName         string
        Department      Department
        DepartmentID    uint //Department id
        EmployeeContact []EmployeeContact //Array of Employee contact
    }
    

    And then this preload should do the trick

    var employee []model.Employee
    
    err := db.Preload("Department").Preload("EmployeeContact").Find(&employee).Error
    

    and them employee parameter should have list of all employees in your system with preloaded relations

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python