diff --git a/src/masoniteorm/collection/Collection.py b/src/masoniteorm/collection/Collection.py index e5b3f960..e238db8a 100644 --- a/src/masoniteorm/collection/Collection.py +++ b/src/masoniteorm/collection/Collection.py @@ -41,7 +41,6 @@ def first(self, callback=None): filtered = self.filter(callback) response = None - # print(filtered._items) if filtered: response = filtered[0] return response diff --git a/src/masoniteorm/query/QueryBuilder.py b/src/masoniteorm/query/QueryBuilder.py index 6a9bb576..2ce2e93d 100644 --- a/src/masoniteorm/query/QueryBuilder.py +++ b/src/masoniteorm/query/QueryBuilder.py @@ -1952,18 +1952,22 @@ def _register_relationships_to_model( self """ if related_result and isinstance(hydrated_model, Collection): - map_related = related_result.group_by(related.foreign_key) + map_related = self._map_related(related_result, related) 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 + def _map_related(self, related_result, related): + if related.__class__.__name__ == 'MorphTo': + return related_result + + return related_result.group_by(related.get_foreign_key()) + def all(self, selects=[], query=False): """Returns all records from the table. diff --git a/src/masoniteorm/relationships/BelongsTo.py b/src/masoniteorm/relationships/BelongsTo.py index 8b54ff6c..b92af119 100644 --- a/src/masoniteorm/relationships/BelongsTo.py +++ b/src/masoniteorm/relationships/BelongsTo.py @@ -63,7 +63,6 @@ def get_related(self, query, relation, eagers=(), callback=None): ).first() def register_related(self, key, model, collection): - 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[0] if related else None}) diff --git a/src/masoniteorm/relationships/HasMany.py b/src/masoniteorm/relationships/HasMany.py index 5b25cc31..a940df04 100644 --- a/src/masoniteorm/relationships/HasMany.py +++ b/src/masoniteorm/relationships/HasMany.py @@ -27,7 +27,6 @@ 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))} + {key: collection.get(getattr(model, self.local_key)) or Collection()} ) diff --git a/src/masoniteorm/relationships/MorphTo.py b/src/masoniteorm/relationships/MorphTo.py index f0abcf44..84ba8bb7 100644 --- a/src/masoniteorm/relationships/MorphTo.py +++ b/src/masoniteorm/relationships/MorphTo.py @@ -50,7 +50,7 @@ def __get__(self, instance, owner): def __getattr__(self, attribute): relationship = self.fn(self)() - return getattr(relationship.builder, attribute) + return getattr(relationship._related_builder, attribute) def apply_query(self, builder, instance): """Apply the query and return a dictionary to be hydrated diff --git a/tests/sqlite/relationships/test_sqlite_relationships.py b/tests/sqlite/relationships/test_sqlite_relationships.py index b182e845..d88039ff 100644 --- a/tests/sqlite/relationships/test_sqlite_relationships.py +++ b/tests/sqlite/relationships/test_sqlite_relationships.py @@ -1,11 +1,8 @@ -import os import unittest - from src.masoniteorm.models import Model from src.masoniteorm.relationships import belongs_to, has_many, has_one, belongs_to_many from tests.integrations.config.database import DB - class Profile(Model): __table__ = "profiles" __connection__ = "dev"