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 54 forks source link

Preload BelongsTo column #155

Closed HTMHell closed 4 years ago

HTMHell commented 4 years ago

Let's take the code from the examples to simplify:

class User {
  @PrimaryKey()
  String id;

  String name;

  @HasOne(AddressBean)
  Address address;

  static const String tableName = '_user';

  String toString() => "User($id, $name, $address)";
}

class Address {
  @PrimaryKey()
  String id;

  @BelongsTo(UserBean)
  String userId;

  String street;

  static String tableName = 'address';

  String toString() => "Post($id, $userId, $street)";
}

@GenBean()
class UserBean extends Bean<User> with _UserBean {
  UserBean(Adapter adapter)
      : addressBean = new AddressBean(adapter),
        super(adapter);

  final AddressBean addressBean;
}

@GenBean()
class AddressBean extends Bean<Address> with _AddressBean {
  AddressBean(Adapter adapter) : super(adapter);
}

How to get User object from AddressBean? final address = await addressBean.find('1', preload: true);

I would like to access something like address.user which will return a User object that has been loaded from the relationship. Is it possible?

jaumard commented 4 years ago

It is not currently possible. Instead you can use UserBean.findByAddress to have it. But I'm 100% for jaguar to support both relation with object in the future :) but it need some work and refactoring so it will be not in near future

jaumard commented 4 years ago

Related issue about this is discuss here https://github.com/Jaguar-dart/jaguar_orm/issues/147