2017-09-26 50 views
2

我正在使用Vapor 2并试图创建非最终模型来对其进行子类化。可能吗?我有下面的代码抽象模型:Vapor 2,Fluent模型子类

class MongoObject: Model, JSONRepresentable { 

    let storage = Storage() 

    let creationDate: Date 

    init() { 
     creationDate = Date() 
    } 

    required init(row: Row) throws { 
     creationDate = try row.get(KeyPath.creationDate) 
    } 

    func makeRow() throws -> Row { 
     var row = Row() 
     try row.set(KeyPath.creationDate, creationDate) 
     return row 
    } 

    func makeJSON() throws -> JSON { 
     var json = JSON() 
     try json.set(KeyPath.id, id) 
     try json.set(KeyPath.creationDate, creationDate) 
     return json 
    } 

} 

extension MongoObject: Preparation { 

    class func prepare(model: Creator) { } 

    static func prepare(_ database: Database) throws { 
     try database.create(self) { (builder) in 
      builder.id() 
      builder.date(KeyPath.creationDate) 
      prepare(model: builder) 
     } 
    } 

    static func revert(_ database: Database) throws { 
     try database.delete(self) 
    } 

} 

,但得到的编译错误:

method 'make(for:)' in non-final class 'MongoObject' must return Self to conform to protocol 'Parameterizable'

回答

1

你非最后的“抽象”的模型,子类是符合ParameterizableModel一致性的一部分。可参数化要求返回Self。默认情况下,这是通过从路径组件中读取实体的ID来实现的。你现在得到的问题是编译器不能为子类返回Self,因为它是在更高级的模型上实现的。

解决方案非常简单,你不能在这里进行子类化。

0

我可能已经太晚了,但是我遇到了完全相同的问题,我所要做的就是让课程最终成功。

method 'make(for:)' in non-final class 'MongoObject' must return Self to conform to protocol 'Parameterizable'