Jaguar-dart / jaguar_orm

Source-generated ORM with relations (one-to-one, one-to-many, many-to-many), preloading, cascading, polymorphic relations, etc
https://jaguar-dart.github.io
BSD 3-Clause "New" or "Revised" License
217 stars 52 forks source link

hasMany update should insert if not exist object #104

Open hjJunior opened 5 years ago

hjJunior commented 5 years ago

I have Order connected by HasMany relation with OrderItem, the insert method looks pretty good, but about update, I think its missing to create item if not exists

Imagine I created a new Order using insert with two OrderItem, so If I remove one OrderItem or maybe insert a new it won't be updated table OrderItem

    if (cascade) {
      Order newModel;
      if (model.orderProducts != null) {
        for (final child in model.orderProducts) {
          await productBean.update(child,
              cascade: cascade, associate: associate);
        }
      }
      if (model.orderItems != null) {
        if (associate) {
          newModel ??= await find(model.id);
          model.orderItems
              .forEach((x) => orderItemBean.associateOrder(x, newModel));
        }
        for (final child in model.orderItems) {
          await orderItemBean.update(child,
              cascade: cascade, associate: associate);
        }
      }
    }