Skip to content

Commit

Permalink
Refactor relationship mapping in QueryBuilder and related classes
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Nov 21, 2024
1 parent 7b3c2f1 commit 1d70063
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1968,12 +1968,7 @@ def _register_relationships_to_model(
return self

def _map_related(self, related_result, related):
if related.__class__.__name__ == 'MorphTo':
return related_result
elif related.__class__.__name__ in ['HasOneThrough', 'HasManyThrough']:
return related_result.group_by(related.local_key)

return related_result
return related.map_related(related_result)

def all(self, selects=[], query=False):
"""Returns all records from the table.
Expand Down
3 changes: 3 additions & 0 deletions src/masoniteorm/relationships/BaseRelationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,6 @@ def query_has(self, current_query_builder, method="where_exists"):
)

return related_builder

def map_related(self, related_result):
return related_result
3 changes: 3 additions & 0 deletions src/masoniteorm/relationships/HasOneThrough.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,6 @@ def get_with_count_query(self, current_builder, callback):
)

return return_query

def map_related(self, related_result):
return related_result.group_by(related.local_key)
3 changes: 3 additions & 0 deletions src/masoniteorm/relationships/MorphTo.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,6 @@ def attach_related(self, current_model, related_record):
raise NotImplementedError(
"MorphTo relationship does not implement the attach_related method"
)

def map_related(self, related_result):
return related_result

0 comments on commit 1d70063

Please sign in to comment.