HasManyThroughAssociation
The HasManyThroughAssociation
is often used to set up a many-to-many connection with another record. This association indicates that the declaring record can be matched with zero or more instances of another record by proceeding through a third record.
struct HasManyThroughAssociation<Origin, Destination>
For example, consider the practice of passport delivery. One country “has many” citizens “through” its passports:
struct Citizen: TableRecord { }
struct Passport: TableRecord {
static let citizen = belongsTo(Citizen.self)
}
struct Country: TableRecord {
static let passports = hasMany(Passport.self)
static let citizens = hasMany(Citizen.self,
through: passports,
using: Passport.citizen)
}
The HasManyThroughAssociation
is also useful for setting up “shortcuts” through nested associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way:
struct Paragraph: TableRecord { }
struct Section: TableRecord {
static let paragraphs = hasMany(Paragraph.self)
}
struct Document: TableRecord {
static let sections = hasMany(Section.self)
static let paragraphs = hasMany(Paragraph.self,
through: sections,
using: Section.paragraphs)
}
As in the examples above, HasManyThroughAssociation
is always built from two other associations. Those associations can be any Association
.