hasToManyInKeyPath method

bool hasToManyInKeyPath(
  1. EntityHandler? entityHandler
)

If any key of the keys path, not only the last one, is a to-many relationship (a field declared as a list of entities or references): roles (the last key), roles.type and owner.roles.type (a key before the last). Then the key's value is a list, one value per related entity.

Resolved by the declared field types of entityHandler's entity (not by the values: a to-many without related entities is an empty list, as an empty list field).

A list field that is not a relationship (List<String> tags, a Uint8List) is not a to-many: its value is compared as a whole.

Cached per entityHandler (see _hasToManyInKeyPathCacheHandler).

Implementation

bool hasToManyInKeyPath(EntityHandler? entityHandler) {
  if (identical(entityHandler, _hasToManyInKeyPathCacheHandler)) {
    return _hasToManyInKeyPathCacheResult ?? false;
  }

  var toMany = false;
  var handler = entityHandler;
  for (var key in keys) {
    if (handler == null || key is! ConditionKeyField) break;

    var type = handler.getFieldType(null, key.name, resolveFiledName: true);
    if (type == null) break;

    if (isToManyType(type, handler)) {
      toMany = true;
      break;
    }

    handler = _resolveValueEntityHandler(type, handler);
  }

  _hasToManyInKeyPathCacheHandler = entityHandler;
  _hasToManyInKeyPathCacheResult = toMany;
  return toMany;
}