auqeduct如何获取属性的长度作为过渡属性?

时间:2019-12-17 15:49:40

标签: dart aqueduct

在我的模型中,作者具有许多 Book 。  是否可以使每个作者获得图书量是暂时属性?

这是我尝试过的,但它说:

  

NoSuchMethodError:在空值上调用了吸气剂“长度”。

我的灵感来自docs

class Author extends ManagedObject<_Author> implements _Author {
  @Serialize(input: false, output: true)
  int get numBooks => books.length;
}

class _Author {

  @primaryKey
  int id;

  @Column(unique: true)
  String name;

  ManagedSet<Book> books;
}

class Book extends ManagedObject<_Book> implements _Book {}

class _Book {

  @primaryKey
  int id;

  @Relate(#books)
  Author author;
}

我正在使用渡槽3.2.1。

1 个答案:

答案 0 :(得分:0)

我认为这不可能,因为book是一个相关字段。

您可以在控制器中使用.join()来获取所有书籍。