match method

  1. @override
List<KhademModel> match(
  1. List<KhademModel> models,
  2. List<Related> results,
  3. String relation
)
override

Match the eagerly loaded results to their parents.

Implementation

@override
List<KhademModel> match(
  List<KhademModel> models,
  List<Related> results,
  String relation,
) {
  final dictionary = <dynamic, List<Related>>{};

  for (final result in results) {
    final key = result.getAttribute('khadem_pivot_foreign_key');
    if (key != null) {
      (dictionary[key] ??= <Related>[]).add(result);
    }
  }

  for (final model in models) {
    final key = model.getAttribute(parentKey);
    if (dictionary.containsKey(key)) {
      model.setRelation(relation, dictionary[key]);
    } else {
      model.setRelation(relation, <Related>[]);
    }
  }

  return models;
}