diff --git a/playground2.py b/playground2.py index 6ef0d07a..60bbdb4a 100644 --- a/playground2.py +++ b/playground2.py @@ -10,6 +10,7 @@ class CreditCard(Model): class Company(Model): __table__ = "tbl_companies" + __primary_key__ = "company_id" @property def cards(self): @@ -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 diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index caab3742..674b1e00 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -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): @@ -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}'") @@ -1210,4 +1219,8 @@ def __call__(self): return self.relationship # def __repr__(self): - # return repr(self.relationship) \ No newline at end of file + # return repr(self.relationship) + + def __iter__(self): + print("iterating relationship propery") + return iter(self.relationship) # Use the iterator of the list \ No newline at end of file diff --git a/src/masoniteorm/models/relationships/new/HasMany.py b/src/masoniteorm/models/relationships/new/HasMany.py index ae8803f0..d31c7e86 100644 --- a/src/masoniteorm/models/relationships/new/HasMany.py +++ b/src/masoniteorm/models/relationships/new/HasMany.py @@ -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 @@ -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}'") \ No newline at end of file + 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 \ No newline at end of file diff --git a/src/masoniteorm/models/relationships/new/__init__.py b/src/masoniteorm/models/relationships/new/__init__.py index 22b1f769..cf8a6ab3 100644 --- a/src/masoniteorm/models/relationships/new/__init__.py +++ b/src/masoniteorm/models/relationships/new/__init__.py @@ -1,3 +1,2 @@ from .HasOne import HasOne -from .HasMany import HasMany -# from .BelongsTo import BelongsTo \ No newline at end of file +from .HasMany import HasMany \ No newline at end of file