hasOneThrough<R extends KhademModel<R>> method

RelationDefinition<KhademModel> hasOneThrough<R extends KhademModel<R>>({
  1. required String throughTable,
  2. required String throughForeignKey,
  3. required String relatedForeignKey,
  4. required String relatedTable,
  5. required R factory(),
  6. String? parentLocalKey,
  7. String throughLocalKey = 'id',
  8. dynamic query(
    1. QueryBuilderInterface
    )?,
})

Define a "has one through" relationship with clearer parameter names.

Key meanings:

  • throughForeignKey: column on the through table that points to the parent. Example: users.country_id.
  • relatedForeignKey: column on the related table that points to the through table. Example: posts.user_id.
  • parentLocalKey: column on the parent model used for matching (default: id). Example: countries.id.
  • throughLocalKey: column on the through table used in the JOIN (default: id). Example: users.id.

Implementation

RelationDefinition hasOneThrough<R extends KhademModel<R>>({
  required String throughTable,
  required String throughForeignKey,
  required String relatedForeignKey,
  required String relatedTable,
  required R Function() factory,
  String? parentLocalKey,
  String throughLocalKey = 'id',
  Function(QueryBuilderInterface)? query,
}) {
  final resolvedParentLocalKey =
      parentLocalKey ?? (this as KhademModel).primaryKey;
  return RelationDefinition<R>(
    type: RelationType.hasOneThrough,
    localKey: resolvedParentLocalKey,
    foreignKey: '',
    relatedTable: relatedTable,
    factory: factory,
    throughTable: throughTable,
    firstKey: throughForeignKey,
    secondKey: relatedForeignKey,
    secondLocalKey: throughLocalKey,
    query: query,
  );
}