1
A
回答
4
我找到了解決辦法。這裏有一個車主擁有很多車的簡單例子,可能會對某人有所幫助。
擁有者:
final class Owner: Model {
static let idKey = "id"
static let nameKey = "name"
static let carsKey = "cars"
var name: String
let storage = Storage()
var cars: Children<Owner, Car> {
return children()
}
init(name: String) {
self.name = name
}
init(row: Row) throws {
name = try row.get(Owner.nameKey)
}
func makeRow() throws -> Row {
var row = Row()
try row.set(Owner.nameKey, name)
return row
}
}
extension Owner: Preparation {
static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id()
builder.string(Owner.nameKey)
}
}
static func revert(_ database: Database) throws {
try database.delete(self)
}
}
extension Owner: JSONConvertible {
convenience init(json: JSON) throws {
try self.init(
name: json.get(Owner.nameKey)
)
}
func makeJSON() throws -> JSON {
var json = JSON()
try json.set(Owner.idKey, id)
try json.set(Owner.nameKey, name)
try json.set(Owner.carsKey, try cars.all())
return json
}
}
extension Owner: ResponseRepresentable { }
extension Owner: Updateable {
public static var updateableKeys: [UpdateableKey<Owner>] {
return [
UpdateableKey(Owner.nameKey, String.self) { owner, text in
owner.name = name
}
]
}
}
汽車:
final class Car: Model {
static let idKey = "id"
static let makeKey = "make"
static let modelKey = "model"
static let ownerIdKey = "owner_id"
var make: String
var model: String
var ownerId: Identifier
let storage = Storage()
var owner: Parent<Car, Owner> {
return parent(id: ownerId)
}
init(make: String, model: String, ownerId: Identifier) {
self.make = make
self.model = model
self.ownerId = ownerId
}
init(row: Row) throws {
make = try row.get(Car.makeKey)
model = try row.get(Car.modelKey)
ownerId = try row.get(Car.ownerIdKey)
}
func makeRow() throws -> Row {
var row = Row()
try row.set(Car.makeKey, make)
try row.set(Car.modelKey, model)
try row.set(Car.ownerIdKey, ownerId)
return row
}
}
extension Car: JSONConvertible {
convenience init(json: JSON) throws {
try self.init(
make: json.get(Car.makeKey),
model: json.get(Car.modelKey),
ownerId: json.get(Car.ownerIdKey)
)
}
func makeJSON() throws -> JSON {
var json = JSON()
try json.set(Car.idKey, id)
try json.set(Car.makeKey, make)
try json.set(Car.modelKey, model)
try json.set(Car.ownerIdKey, ownerId)
return json
}
}
extension Car: ResponseRepresentable {}
extension Car: Preparation {
static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id()
builder.string(Car.makeKey)
builder.string(Car.modelKey)
builder.foreignId(for: Owner.self)
}
}
static func revert(_ database: Database) throws {
try database.delete(self)
}
}
extension Car: Updateable {
public static var updateableKeys: [UpdateableKey<Car>] {
return [
UpdateableKey(Car.makeKey, String.self) { car, make in
car.make = make
}
]
}
}
相關問題
- 1. JPA 2.x單向一對多關係
- 2. Doctrine2不能使'2'一對多關係
- 3. Symfony的2:學說一對多關係
- 4. 學說2:一對多關係
- 5. 學說2一對多的關係
- 6. 蒸氣Xcode項目結構關
- 7. 蒸氣驗證器
- 8. 一對多關係
- 9. 一對多關係
- 10. 多對多設計,2種關於同一物體的關係?
- 11. 基於一對一,多對多,多對多的關係計算2個表
- 12. 發揮框架2多對多關係
- 13. symfony 2多對多關係crud
- 14. Doctrine 2中可空多對多關係
- 15. 學說2多對多關係
- 16. Zend 2 Framework中的多對多關係
- 17. symfony 2多對多的關係
- 18. 續集2多對多的關係
- 19. 一個多對多關係
- 20. 從一對一關係轉變爲多對多關係
- 21. SQL一對多關係,但多對一表關係
- 22. Django多對一/一對多關係
- 23. 多對多一對一的關係SSAS
- 24. 通過一對多關係檢索多對多關係
- 25. 變換多對多關係一對多關係
- 26. 多對多關係與一對多關係有什麼區別?
- 27. 關係數據庫一對多關係
- 28. 蒸氣沉積故障
- 29. 在蒸氣上使用Bootstrap
- 30. 蒸氣服務器錯誤
[氣相實例實驗室(https://github.com/VaporExamplesLab)具有蒸氣舊版本蒸氣示例的遷移2 + Swift 4.「一對多」(父 - 子)關係示例位於['ServerSideSwift_J_RelationsAwt'](https://github.com/VaporExamplesLab/ServerSideSwift_J_RelationsAwt)示例中。 –