Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Nov 24, 2024
1 parent f23adbd commit de1df45
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class Model(TimeStampsMixin, ObservesEvents, metaclass=ModelMeta):
__resolved_connection__ = None
__selects__ = []

__relationships__ = {}

__observers__ = {}
__has_events__ = True

Expand Down
19 changes: 12 additions & 7 deletions src/masoniteorm/models/relationships/new/HasOne.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ def __init__(self, model_class, foreign_key=None, local_key=None):
self.local_key = local_key
self.owner = None

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

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


def __call__(self, owner):
"""Fetch the related record when invoked."""
related_model = self.model_class
# print("related model", related_model)
print(f"Method called: {method_name}")
related_model.owner = self
self.owner = owner
foreign_key_value = owner.__attributes__.get(self.foreign_key)
foreign_key_value = owner.__attributes__.get(self.local_key)
if not foreign_key_value:
return None
# builder = related_model.builder.where(self.local_key, foreign_key_value)
print("No foreign key value")
return self
print("Fetching has one", owner.__relationships__)
builder = self.apply_query(related_model.builder)
result = builder.first()
self.owner = owner
Expand Down
8 changes: 8 additions & 0 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,10 @@ def prepare_result(self, result, collection=False):
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)

self._register_relationships_to_model(
Expand All @@ -1941,6 +1945,7 @@ def prepare_result(self, result, collection=False):
else:
return result or None

# TODO: move this out of query builder
def _register_relationships_to_model(
self, related, related_result, hydrated_model, relation_key
):
Expand All @@ -1956,6 +1961,7 @@ def _register_relationships_to_model(
Returns:
self
"""

if related_result and isinstance(hydrated_model, Collection):
map_related = self._map_related(related_result, related)
for model in hydrated_model:
Expand All @@ -1964,6 +1970,8 @@ def _register_relationships_to_model(
else:
model.add_relation({relation_key: map_related or None})
else:
hydrated_model.__relationships__.update({relation_key: related_result or None})
# print("register related", relation_key, hydrated_model.__relationships__)
hydrated_model.add_relation({relation_key: related_result or None})
return self

Expand Down
34 changes: 34 additions & 0 deletions tests/models/relationships/test_relationships.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import unittest
from src.masoniteorm.models import Model

class User(Model):
__connection__ = "t"

@property
def profile(self):
return self.has_one(Profile, "user_id", "id")

class Profile(Model):
__connection__ = "t"


class TestRelatinships(unittest.TestCase):

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

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

def test_can_get_has_one_query_builder(self):
user = User.find(1)
self.assertEqual(user.name, "bill")
self.assertEqual(user.profile().to_sql(), 'SELECT * FROM "profiles" WHERE "profiles"."user_id" = \'1\'')

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')

0 comments on commit de1df45

Please sign in to comment.