代码之家  ›  专栏  ›  技术社区  ›  Udayakumar

如何获取gorm中的struct(inside struct)值

  •  2
  • Udayakumar  · 技术社区  · 6 年前

    我是刚从戈兰和戈姆来的。我有一些问题。如何获取内部结构值?(就像golang中的嵌套结构一样),我尝试过,但没有得到实际结果。

    我有三个结构

    部门结构

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

    员工结构

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

    员工联系人

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

    关系

    #部门是员工的父级。

    #员工是员工联系人的父级。

    我用了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))
    

    它将返回 output

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

    我需要,当我通过员工 id 它将按以下格式返回

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

    有人能教我吗?我怎样才能做到。 谢谢。

    1 回复  |  直到 6 年前
        1
  •  1
  •   ttomalak    6 年前

    首先,你可能希望你的员工模式能像这样

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

    然后这个预加载荷就可以了

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

    其中,Employee参数应该包含系统中所有具有预加载关系的员工的列表。