hasMany<T extends Model> method

Future<List<T>> hasMany<T extends Model>(
  1. ModelFactory<T> factory, {
  2. String? foreignKey,
  3. String? localKey,
})

Defines a "Has Many" relationship.

Implementation

Future<List<T>> hasMany<T extends Model>(ModelFactory<T> factory, {String? foreignKey, String? localKey}) async {
  final fk = foreignKey ?? '${runtimeType.toString().toLowerCase()}_id';
  final targetKey = localKey ?? 'id';

  final idValue = getAttribute(targetKey) ?? id;
  if (idValue == null) return [];

  final rows = await QueryBuilder(_inferTableName<T>(), db)
      .where(fk, '=', idValue)
      .get();

  return rows.map((row) {
    final m = factory(row);
    m.setRawData(db, row);
    return m;
  }).toList();
}