Skip to content

Commit

Permalink
fixed tests and made attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Apr 3, 2024
1 parent 29426df commit a6a5eea
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 10 deletions.
Binary file modified orm.sqlite3
Binary file not shown.
11 changes: 9 additions & 2 deletions src/masoniteorm/collection/Collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def first(self, callback=None):
if callback:
filtered = self.filter(callback)
response = None

# print(filtered._items)
if filtered:
response = filtered[0]
return response
Expand Down Expand Up @@ -343,7 +345,7 @@ def _serialize(item):

def add_relation(self, result=None):
for model in self._items:
model.add_relations(result or {})
model.add_relation(result or {})

return self

Expand Down Expand Up @@ -532,8 +534,13 @@ def __eq__(self, other):
def __getitem__(self, item):
if isinstance(item, slice):
return self.__class__(self._items[item])
if isinstance(item, dict):
return self._items.get(item, None)

return self._items[item]
try:
return self._items[item]
except KeyError:
return None

def __setitem__(self, key, value):
self._items[key] = value
Expand Down
6 changes: 4 additions & 2 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1951,14 +1951,16 @@ def _register_relationships_to_model(
Returns:
self
"""
map_related = related_result.group_by(related.foreign_key)
if isinstance(hydrated_model, Collection):
if related_result and isinstance(hydrated_model, Collection):
map_related = related_result.group_by(related.foreign_key)
for model in hydrated_model:
if isinstance(related_result, Collection):
print("aa", related)
related.register_related(relation_key, model, map_related)
else:
model.add_relation({relation_key: related_result or None})
else:
print("heeee", related_result)
hydrated_model.add_relation({relation_key: related_result or None})
return self

Expand Down
7 changes: 3 additions & 4 deletions src/masoniteorm/relationships/BelongsTo.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def get_related(self, query, relation, eagers=(), callback=None):
).first()

def register_related(self, key, model, collection):
related = collection.where(
self.foreign_key, getattr(model, self.local_key)
).first()
print('ooooo', collection._items, "tttt", model.serialize(), model, getattr(model, self.local_key))
related = collection.get(getattr(model, self.local_key), None)

model.add_relation({key: related or None})
model.add_relation({key: related[0] if related else None})
1 change: 1 addition & 0 deletions src/masoniteorm/relationships/HasMany.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def set_keys(self, owner, attribute):
return self

def register_related(self, key, model, collection):
print("zzzzzz", getattr(model, self.local_key), collection._items)
model.add_relation(
{key: collection.get(getattr(model, self.local_key))}
)
2 changes: 1 addition & 1 deletion tests/User.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class User(Model):

__fillable__ = ["name", "email", "password"]

__connection__ = "mysql"
__connection__ = "t"

__auth__ = "email"

Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/config/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"log_queries": True,
"propagate": False,
},
"t": {"driver": "sqlite", "database": "ormtestreg.sqlite3", "log_queries": True},
"t": {"driver": "sqlite", "database": "orm.sqlite3", "log_queries": True},
"devprod": {
"driver": "mysql",
"host": os.getenv("MYSQL_DATABASE_HOST"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ def test_with_multiple_per_same_relation(self):
builder = self.get_builder()
result = User.with_("articles", "articles.logo").where("id", 1).first()
self.assertTrue(result.serialize()["articles"])
print("pppppp", result.serialize()["articles"][0])
self.assertTrue(result.serialize()["articles"][0]["logo"])

0 comments on commit a6a5eea

Please sign in to comment.