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 6cedb87 commit f7c6b65
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
10 changes: 8 additions & 2 deletions playground2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CreditCard(Model):

class Company(Model):
__table__ = "tbl_companies"
__primary_key__ = "company_id"

@property
def cards(self):
Expand Down Expand Up @@ -41,7 +42,12 @@ def company(self):

# # Call the relationship instance to get the related company
# related_company_callable = user.company().get()
company = user.company
print(company.cards().to_sql())
# cards = user.company.cards
company = Company.find(373849)

print(company.cards)
for card in company.cards:
print(card)
# print(company.cards().where("is_default", 1).get())
# print("callable", isinstance(user.company(), HasOne)) # Output: True

17 changes: 15 additions & 2 deletions src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@ class RelationshipProperty:
A wrapper for dual behavior: as a property and as a callable returning the relationship instance.
"""
def __init__(self, relationship):
print("init relationship property", relationship)
self.relationship = relationship

def __getattr__(self, name):
Expand All @@ -1196,7 +1197,15 @@ def __getattr__(self, name):
this is returned when you do model.relationship.name
"""
related_instance = self.relationship.get()

try:
return getattr(self.relationship, name)
except AttributeError:
pass

related_instance = self.relationship.get()

print("Delegating attribute access to the related model instance", name)
if related_instance:
return getattr(related_instance, name)
raise AttributeError(f"{self.__class__.__name__} has no attribute from relation '{name}'")
Expand All @@ -1210,4 +1219,8 @@ def __call__(self):
return self.relationship

# def __repr__(self):
# return repr(self.relationship)
# return repr(self.relationship)

def __iter__(self):
print("iterating relationship propery")
return iter(self.relationship) # Use the iterator of the list
19 changes: 16 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,6 @@ def __call__(self):
print("calling")
return self.relationship.apply_query().get()

# 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 @@ -45,7 +43,22 @@ def __getattr__(self, name, *args, **kwargs):
this is returned when you do model.relationship().where(...)
"""


try:
return getattr(self.related_model, name)
except AttributeError:
pass

related_instance = self.apply_query()

if related_instance:
return getattr(related_instance, name)
raise AttributeError(f"{self.__class__.__name__} has no attribute '{name}'")
raise AttributeError(f"{self.__class__.__name__} has no attribute '{name}'")

def __iter__(self):
"""
This is called when iterating over the relationship class
"""
print("iterating")
return iter(self.apply_query().get()) # Use the iterator of the list
3 changes: 1 addition & 2 deletions src/masoniteorm/models/relationships/new/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from .HasOne import HasOne
from .HasMany import HasMany
# from .BelongsTo import BelongsTo
from .HasMany import HasMany

0 comments on commit f7c6b65

Please sign in to comment.