Skip to content

Commit

Permalink
added nested eager
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Nov 24, 2024
1 parent df2d78e commit 70d7bfb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
7 changes: 4 additions & 3 deletions src/masoniteorm/models/relationships/new/HasOne.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ def __init__(self, model_class, foreign_key=None, local_key=None, method=None):
self.method = method
self.owner = None

def apply_query(self, builder, foreign_key_value=None):
def apply_query(self, builder, foreign_key_value=None, eager=None):
owner = self.owner
if not foreign_key_value:
foreign_key_value = owner.__attributes__.get(self.local_key)
builder = builder.where(self.foreign_key, foreign_key_value)
builder = builder.where(self.foreign_key, foreign_key_value).with_(eager or [])
return builder

def get_related(self, foreign, result):
def get_related(self, foreign, result, eager=None):
return self.apply_query(self.model_class, getattr(result, self.local_key)).first()


Expand All @@ -31,6 +31,7 @@ def __call__(self, owner):
return self

self.owner = owner
# print("relationships", owner, owner._relationships)
if self.method and self.method in owner._relationships:
return owner._relationships[self.method]
builder = self.apply_query(related_model.builder)
Expand Down
35 changes: 11 additions & 24 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,6 @@ def get_primary_key(self):

def prepare_result(self, result, collection=False):
if self._model and result:
# eager load here
hydrated_model = self._model.hydrate(result)
if (
self._eager_relation.eagers
Expand All @@ -1899,34 +1898,22 @@ def prepare_result(self, result, collection=False):
# Nested
for relation, eagers in eager_load.items():
callback = None
if inspect.isclass(self._model):
related = getattr(self._model, relation)
elif callable(eagers):
related = getattr(self._model, relation)
callback = eagers
else:
related = self._model.get_related(relation)

result_set = related.get_related(
self, hydrated_model, eagers=eagers, callback=callback
)

print(relation, eagers)
related = getattr(self._model, relation)
# Has one from User -> Profile
result_set = related.get_related(self, hydrated_model)
self._register_relationships_to_model(
related,
result_set,
hydrated_model,
relation_key=relation,
related, result_set, hydrated_model, relation_key=relation
)
for eager in eagers:
inner_related = getattr(result_set, eager).__dict__["related"]
inner_result = inner_related.get_related(self, result_set)
self._register_relationships_to_model(
inner_related, inner_result, result_set, relation_key=eager
)
else:
# Not Nested
for eager in eager_load:
if inspect.isclass(self._model):
related = getattr(self._model, eager)
else:
related = self._model.get_related(eager)

# print(getattr(self._model, eager))
# print(hydrated_model.__dict__['related'])
related = getattr(self._model, eager)

result_set = related.get_related(self, hydrated_model)
Expand Down
25 changes: 24 additions & 1 deletion tests/models/relationships/test_relationships.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def profile(self):
class Profile(Model):
__connection__ = "t"

@property
def user(self):
return self.has_one(User, "id", "user_id")



class TestRelatinships(unittest.TestCase):

Expand All @@ -31,4 +36,22 @@ def test_can_get_has_one_query_builder(self):
def test_can_get_eager_load_from_builder(self):
user = User.with_("profile").find(1)
self.assertEqual(user.name, "bill")
self.assertEqual(user.profile.title, 'title')
self.assertEqual(user.profile.title, 'title')

def test_can_get_nested_eager_load_from_builder(self):
user = User.with_("profile.user").find(1)
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")
self.assertEqual(user.profile.user.name, "bill")

0 comments on commit 70d7bfb

Please sign in to comment.