Skip to content

Commit 2127099

Browse files
committed
WIP
1 parent 928c5d4 commit 2127099

File tree

9 files changed

+59
-54
lines changed

9 files changed

+59
-54
lines changed

orm.sqlite3

0 Bytes
Binary file not shown.

src/masoniteorm/collection/Collection.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,15 @@ def __call__(self, *args):
586586
if not model:
587587
return self
588588

589-
related = self._items[0].__dict__['related']
590-
return related.apply_query(self._items[0].builder)
589+
if hasattr(self._items[0], 'related'):
590+
related = self._items[0].__dict__['related']
591+
return related.apply_query(self._items[0].builder)
592+
else:
593+
return self
594+
595+
def to_qmark(self):
596+
print("to qmark", self._items)
597+
return self
591598

592599
def get_related(self):
593600
print("getting related", self._builder)

src/masoniteorm/expressions/expressions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ class SubSelectExpression:
7676

7777
def __init__(self, builder):
7878
self.builder = builder
79+
if hasattr(builder, 'to_qmark'):
80+
self._bindings = builder._items
81+
else:
82+
self._bindings = []
7983

8084

8185
class SubGroupExpression:

src/masoniteorm/models/relationships/new/BelongsTo.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,18 @@ def apply_query(self, builder, foreign_key_value=None, eager=None):
1919
def get_related(self, foreign, result, eager=None):
2020
return self.apply_query(self.model_class, getattr(result, self.local_key)).first()
2121

22-
2322
def __call__(self, owner):
24-
"""Fetch the related record when invoked."""
23+
"""Return a query builder for the relationship."""
2524
related_model = self.model_class
26-
related_model.owner = self
2725
self.owner = owner
26+
27+
# Get the foreign key value
2828
foreign_key_value = owner.__attributes__.get(self.local_key)
2929
if not foreign_key_value:
30-
print("No foreign key value")
31-
return self
32-
33-
self.owner = owner
34-
# print("relationships", owner, owner._relationships)
35-
if self.method and self.method in owner._relationships:
36-
return owner._relationships[self.method]
37-
builder = self.apply_query(related_model.builder)
38-
result = builder.first()
39-
self.owner = owner
40-
result.__dict__['related'] = self
41-
return result
42-
30+
return related_model.builder.where_raw("1 = 0") # Return empty query if no foreign key
31+
32+
# Return the query builder directly
33+
return self.apply_query(related_model.builder, foreign_key_value)
4334

4435
def add_relation(self, model_instance, result, relation_key=None):
45-
# if result is a collection, do a where
46-
return model_instance.add_relation({relation_key: result or None})
36+
return model_instance.add_relation({relation_key: result or None})

src/masoniteorm/models/relationships/new/HasMany.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,17 @@ def get_related(self, foreign, result, eager=None):
3131
return self.apply_query(self.model_class, getattr(result, self.foreign_key)).get()
3232

3333
def __call__(self, owner):
34-
"""Fetch the related record when invoked."""
34+
"""Return a query builder for the relationship."""
3535
related_model = self.model_class
36-
# print("related model", related_model)
37-
related_model.owner = self
3836
self.owner = owner
39-
foreign_key_value = owner.__attributes__.get(self.foreign_key)
37+
38+
# Get the foreign key value
39+
foreign_key_value = owner.__attributes__.get(self.local_key)
4040
if not foreign_key_value:
41-
return self
42-
43-
if self.method and self.method in owner._relationships:
44-
return owner._relationships[self.method]
45-
result = self.apply_query(related_model.builder).get()
46-
result._builder = self.apply_query(self.model_class.builder, foreign_key_value)
47-
result._related = self
48-
for item in result:
49-
item.__dict__['related'] = self
50-
return result
41+
return related_model.builder.where_raw("1 = 0") # Return empty query if no foreign key
42+
43+
# Return the query builder directly
44+
return self.apply_query(related_model.builder, foreign_key_value)
5145

5246
def add_relation(self, model_instance, result, relation_key=None):
5347
# if result is a collection, do a where

src/masoniteorm/models/relationships/new/HasOne.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,18 @@ def apply_query(self, builder, foreign_key_value=None, eager=None):
1919
def get_related(self, foreign, result, eager=None):
2020
return self.apply_query(self.model_class, getattr(result, self.local_key)).first()
2121

22-
2322
def __call__(self, owner):
24-
"""Fetch the related record when invoked."""
23+
"""Return a query builder for the relationship."""
2524
related_model = self.model_class
26-
related_model.owner = self
2725
self.owner = owner
26+
27+
# Get the foreign key value
2828
foreign_key_value = owner.__attributes__.get(self.local_key)
2929
if not foreign_key_value:
30-
print("No foreign key value")
31-
return self
32-
33-
self.owner = owner
34-
# print("relationships", owner, owner._relationships)
35-
if self.method and self.method in owner._relationships:
36-
return owner._relationships[self.method]
37-
builder = self.apply_query(related_model.builder)
38-
result = builder.first()
39-
self.owner = owner
40-
result.__dict__['related'] = self
41-
return result
42-
30+
return related_model.builder.where_raw("1 = 0") # Return empty query if no foreign key
31+
32+
# Return the query builder directly
33+
return self.apply_query(related_model.builder, foreign_key_value)
4334

4435
def add_relation(self, model_instance, result, relation_key=None):
45-
# if result is a collection, do a where
46-
return model_instance.add_relation({relation_key: result or None})
36+
return model_instance.add_relation({relation_key: result or None})

src/masoniteorm/query/QueryBuilder.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,3 +2345,20 @@ def oldest(self, *fields):
23452345

23462346
def value(self, column: str):
23472347
return self.get().first()[column]
2348+
2349+
def add_relation(self, model_instance, result, relation_key=None):
2350+
"""Add a relationship to a model instance.
2351+
2352+
Arguments:
2353+
model_instance {Model} -- The model instance to add the relationship to
2354+
result {Model|Collection} -- The related model or collection
2355+
relation_key {string} -- The name of the relationship
2356+
2357+
Returns:
2358+
Model -- The model instance with the relationship added
2359+
"""
2360+
if not hasattr(model_instance, '_relationships'):
2361+
model_instance._relationships = {}
2362+
2363+
model_instance._relationships[relation_key] = result
2364+
return model_instance

src/masoniteorm/query/grammars/BaseGrammar.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,11 @@ def process_wheres(self, qmark=False, strip_first_where=False):
658658
sql_string = self.where_group_string()
659659
elif isinstance(value, SubSelectExpression):
660660
if qmark:
661+
print(value)
661662
query_from_builder = value.builder.to_qmark()
662-
if value.builder._bindings:
663+
if hasattr(value.builder, '_items'):
664+
self.add_binding(*value.builder._items)
665+
else:
663666
self.add_binding(*value.builder._bindings)
664667
else:
665668
query_from_builder = value.builder.to_sql()

tests/sqlite/relationships/test_sqlite_relationships.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_casting(self):
124124
user
125125

126126
def test_setting(self):
127-
users = User.with_("articles").where("is_admin", True).get()
127+
users = User.where("is_admin", True).get()
128128
for user in users:
129129
user.name = "Joe"
130130
user.is_admin = 1

0 commit comments

Comments
 (0)