如何在Vapor中将关系子级包装为数组?

时间:2019-12-24 16:14:21

标签: swift vapor

我有一个亲子关系,孩子们需要包装成数组怎么办?


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)
    }
}

分配给错误:

  

不能将类型为'Children'的值分配为类型为[[TestPrice]'

1 个答案:

答案 0 :(得分:1)

您可以使用查询形成Future数组,然后map数组。假设您在某个控制器/路由中,其中event包含适当的Event,而release包含适当的Release,请尝试以下操作:

{
    release, event in

    _ = release.testPrices.query(on:request).all().map { testP in
        // testP is now [TestPrice]
        event.testPrices = testP
    }
}