Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Nov 23, 2024
1 parent 723d409 commit 6cedb87
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,8 @@ def __init__(self, relationship):
def __getattr__(self, name):
"""
Delegate attribute access to the related model instance.
this is returned when you do model.relationship.name
"""
related_instance = self.relationship.get()
if related_instance:
Expand All @@ -1202,9 +1204,10 @@ def __getattr__(self, name):
def __call__(self):
"""
Make the relationship callable to return the relationship instance.
this is returned when you do model.relationship()
"""
print("Calling relationship")
return self.relationship.apply_query()
return self.relationship

# def __repr__(self):
# return repr(self.relationship)
17 changes: 14 additions & 3 deletions src/masoniteorm/models/relationships/new/HasMany.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def __call__(self):
print("calling")
return self.relationship.apply_query().get()

def where(self, key):
return self.related_model.where(key, value)
# def where(self, key):
# return self.related_model.where(key, value)

def apply_query(self):
"""Apply the query and return a dictionary to be hydrated
Expand All @@ -37,4 +37,15 @@ def apply_query(self):
dict -- A dictionary of data which will be hydrated.
"""
print("applying query has many")
return self.related_model.where(self.foreign_key, getattr(self.parent, self.foreign_key))
return self.related_model.where(self.foreign_key, getattr(self.parent, self.foreign_key))

def __getattr__(self, name, *args, **kwargs):
"""
this is called when accesssing query builder methods on the relationship class
this is returned when you do model.relationship().where(...)
"""
related_instance = self.apply_query()
if related_instance:
return getattr(related_instance, name)
raise AttributeError(f"{self.__class__.__name__} has no attribute '{name}'")

0 comments on commit 6cedb87

Please sign in to comment.