以下代码完美运行,我得到了我所需要的,但我很好奇是否有更好的方法来返回连接表。如果你看下面,我需要为加入的#34;地址"写多少代码?表。我希望有人能证实这是最好的方法。谢谢!
static func findByEntityId(entityId: Int) throws -> [EntityAddress] {
let query = table.filter(EntityId == entityId).join(AddressDB.table, on: AddressDB.table[Id] == table[AddressId])
var array = [T]()
let items = try db.prepare(query)
for item in items {
let address = Address(id: item[AddressDB.table[AddressDB.Id]], addressType: item[AddressDB.table[AddressDB.AddressType]], addressStatus: item[AddressDB.table[AddressDB.AddressStatus]], address1: item[AddressDB.table[AddressDB.Address1]], address2: item[AddressDB.table[AddressDB.Address2]], city: item[AddressDB.table[AddressDB.City]], state: item[AddressDB.table[AddressDB.State]], zip: item[AddressDB.table[AddressDB.Zip]], country: item[AddressDB.table[AddressDB.Country]], latitude: item[AddressDB.table[AddressDB.Latitude]], longitude: item[AddressDB.table[AddressDB.Longitude]], isDeleted: item[AddressDB.table[AddressDB.IsDeleted]], created: item[AddressDB.table[AddressDB.Created]], createdBy: item[AddressDB.table[AddressDB.CreatedBy]]!, modified: item[AddressDB.table[AddressDB.Modified]], modifiedBy: item[AddressDB.table[AddressDB.ModifiedBy]]!)
array.append(EntityAddress(id: item[table[Id]], addressId: item[AddressId], entityId: item[EntityId], entityType: item[EntityType], address: address, isDeleted: item[table[IsDeleted]], created: item[table[Created]], createdBy: item[table[CreatedBy]] ?? 0, modified: item[table[Modified]], modifiedBy: item[table[ModifiedBy]] ?? 0))
}
return array
}