普通网友 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 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?