Skip to content

Commit

Permalink
Merge pull request #880 from MasoniteFramework/fix/relationship-perfo…
Browse files Browse the repository at this point in the history
…rmance-joe

Fix/relationship performance joe attempt
  • Loading branch information
josephmancuso authored Apr 6, 2024
2 parents f963387 + 41c49c4 commit d6f2528
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 15 deletions.
Binary file modified orm.sqlite3
Binary file not shown.
10 changes: 8 additions & 2 deletions src/masoniteorm/collection/Collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def first(self, callback=None):
if callback:
filtered = self.filter(callback)
response = None

if filtered:
response = filtered[0]
return response
Expand Down Expand Up @@ -343,7 +344,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 +533,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
11 changes: 9 additions & 2 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1951,16 +1951,23 @@ def _register_relationships_to_model(
Returns:
self
"""
if isinstance(hydrated_model, Collection):
if related_result and isinstance(hydrated_model, Collection):
map_related = self._map_related(related_result, related)
for model in hydrated_model:
if isinstance(related_result, Collection):
related.register_related(relation_key, model, related_result)
related.register_related(relation_key, model, map_related)
else:
model.add_relation({relation_key: related_result or None})
else:
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.foreign_key)

def all(self, selects=[], query=False):
"""Returns all records from the table.
Expand Down
6 changes: 2 additions & 4 deletions src/masoniteorm/relationships/BelongsTo.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ 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()
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})
2 changes: 1 addition & 1 deletion src/masoniteorm/relationships/HasMany.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ def set_keys(self, owner, attribute):

def register_related(self, key, model, collection):
model.add_relation(
{key: collection.where(self.foreign_key, getattr(model, self.local_key))}
{key: collection.get(getattr(model, self.local_key)) or Collection()}
)
2 changes: 1 addition & 1 deletion src/masoniteorm/relationships/MorphTo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
3 changes: 0 additions & 3 deletions tests/sqlite/relationships/test_sqlite_relationships.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit d6f2528

Please sign in to comment.