matchOneOrMany method
List<KhademModel>
matchOneOrMany(
- List<
KhademModel> models, - List<
Related> results, - String relation,
- String type,
Match the eagerly loaded results to their parents.
Implementation
List<KhademModel> matchOneOrMany(
List<KhademModel> models,
List<Related> results,
String relation,
String type,
) {
final dictionary = <dynamic, List<Related>>{};
for (final result in results) {
final key = result.getAttribute(foreignKey);
if (key != null) {
if (!dictionary.containsKey(key)) {
dictionary[key] = [];
}
dictionary[key]!.add(result);
}
}
for (final model in models) {
final key = model.getAttribute(localKey);
if (dictionary.containsKey(key)) {
final value = dictionary[key]!;
if (type == 'one') {
model.setRelation(relation, value.isNotEmpty ? value.first : null);
} else {
model.setRelation(relation, value);
}
} else {
if (type == 'one') {
model.setRelation(relation, null);
} else {
model.setRelation(relation, <Related>[]);
}
}
}
return models;
}