首页 > 解决方案 > 访问辅助实体的属性

问题描述

任何人都可以帮助我解决以下问题吗?在下面的 Gosu 代码中,如何在查询结果中包含地址表中的城市信息(无需翻转表格 - 从地址表开始)?地址表有一个指向用户表的外键,反之则没有外键。行查询可以做到吗?非常感谢

uses gw.api.database.Query

// -- query the User entity --
var queryUser = Query.make(User)

// -- select only User instances who last updated addresses in the city of Chicago --
var tableAddress = queryUser.join(Address, "UpdateUser")
tableAddress.compare("City", Equals, "Chicago") 

// -- fetch the User instances with a for loop and print them --
var result = queryUser.select()

for (user in result) {
    print (user.DisplayName)
}

标签: gosuguidewire

解决方案


既然 Address 有用户 FK 为什么不扭转局面呢?

uses gw.api.database.Query

// -- query the Address entity --
var queryAddress = Query.make(Address)
queryAddress.compare("City", Equals, "Chicago") 

// -- select only User instances who last updated addresses in the city of Chicago --
var tableUser = queryAddress.join(Address#UpdateUser)

// -- fetch the Addresses instances with a for loop and print them --
var result = tableUser.select()

for (address in result) {
    print(address.City?.DisplayName)
    print(address.UpdateUser?.DisplayName)
}

推荐阅读