如何将关系 children 包装到 Vapor 中的数组中?
How to wrap relationship children into array in Vapor?
我有一个 parent-child 关系,children 需要包装到数组中,我该怎么做?
event.testPrices = release.testPrices
final class Event: Content {
var id: String
var inProgress: Bool?
var name: String
var purpose: String?
var testPrices: [TestPrice]
init(id: String, name: String) {
self.id = id
self.name = name
}
}
extension Release {
var testPrices: Children<Release, TestPrice> {
return children(\.releaseId)
}
}
赋值给出错误:
Cannot assign value of type 'Children' to type '[TestPrice]'
您可以使用查询形成 Future
数组,然后 map
它。假设您在某些 controller/route 中,其中 event
包含适当的 Event
并且 release
包含适当的 Release
,试试这个:
{
release, event in
_ = release.testPrices.query(on:request).all().map { testP in
// testP is now [TestPrice]
event.testPrices = testP
}
}
我有一个 parent-child 关系,children 需要包装到数组中,我该怎么做?
event.testPrices = release.testPrices
final class Event: Content {
var id: String
var inProgress: Bool?
var name: String
var purpose: String?
var testPrices: [TestPrice]
init(id: String, name: String) {
self.id = id
self.name = name
}
}
extension Release {
var testPrices: Children<Release, TestPrice> {
return children(\.releaseId)
}
}
赋值给出错误:
Cannot assign value of type 'Children' to type '[TestPrice]'
您可以使用查询形成 Future
数组,然后 map
它。假设您在某些 controller/route 中,其中 event
包含适当的 Event
并且 release
包含适当的 Release
,试试这个:
{
release, event in
_ = release.testPrices.query(on:request).all().map { testP in
// testP is now [TestPrice]
event.testPrices = testP
}
}