flutter - 对象盒 Dart : how to filter based on a ToOne relation?

标签 flutter dart objectbox

假设我有

@Entity()
class Order {
  int id;
  final item = ToOne<Item>();
  final customer = ToOne<Customer>();
}

@Entity()
class Item {
  int id;
  int price;
  @Backlink()
  final orders = ToMany<Order>();
}

如何根据商品价格查询过滤订单。我知道我可以查询商品并获取反向链接订单,但反过来也可能吗?例如:

final orders = store.box<Order>().query(Order_.item.price < 100).build(). 

docs说即使跨关系过滤数据,但我找不到方法来做到这一点。

最佳答案

要按型号的商品价格查询订单,您可以使用以下命令:

final ordersQuery = store.box<Order>().query()
  ..link(Order_.item, Item_.price < 100)
  ..build();

它的作用是:

  • 首先创建一个QueryBuilder<Order>无条件(query() 没有参数)
  • 然后创建一个指向 Item 的链接(如果我们想要深度链接另一个实体,它会创建另一个查询生成器,但在本例中我们不需要)
  • 然后调用build()关于“根”QueryBuilder ,创建Query<Order>

前面的代码相当于:

final ordersQueryBuilder = store.box<Order>().query();
ordersQueryBuilder.link(Order_.item, Item_.price < 100);
final ordersQuery = ordersQueryBuilder.build();

然后,使用任一版本的代码,您都可以照常使用查询:

final orders = ordersQuery.find();

// As usual, don't forget to close the query to free up resources when you don't
// need it anymore. In case you missed it, queries are reusable so you can call 
// as many functions on ordersQuery as needed (it will work until you close()).
ordersQuery.close();

关于flutter - 对象盒 Dart : how to filter based on a ToOne relation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66594682/

相关文章:

date - 从字符串中获取日期并在Flutter Dart中转换为Date对象

authentication - 如何进行基本的 http 身份验证(登录和注册)并存储 token

android - Flutter Page 每次在导航弹出时都会重新加载

flutter - 如何在 flutter 中使用 Dot Indicator 创建图像 slider ?

android - ObjectBox 惰性列表行为

macos - ObjectBox Flutter MacOS

button - 如何在 IconButton 上产生圆形波纹效果?

dart - AngularDart ng-更改

dart - 如何访问特定容器的颜色?

flutter - 将 ObjectBox/Instantiate store 和 box 与 Flutter 2.5 和 Riverpod 一起使用