generated from MasoniteFramework/starter-package
-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
266f696
commit f23adbd
Showing
6 changed files
with
108 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/masoniteorm/models/relationships/new/BaseRelationship.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class BaseRelationship: | ||
def init(self, model_class, local_key=None, foreign_key=None): | ||
self.model_class = model_class | ||
self.local_key = local_key | ||
self.foreign_key = foreign_key | ||
|
||
def __call__(self, owner): | ||
"""Create and return the relationship query.""" | ||
related_model = self.model_class() | ||
return self.apply_query(related_model.builder, owner) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from .BaseRelationship import BaseRelationship | ||
|
||
class HasMany(BaseRelationship): | ||
"""Belongs To Relationship Class.""" | ||
|
||
def __init__(self, model_class, foreign_key=None, local_key=None): | ||
self.model_class = model_class | ||
self.foreign_key = foreign_key | ||
self.local_key = local_key | ||
self.owner = None | ||
|
||
def apply_query(self, builder): | ||
owner = self.owner | ||
foreign_key_value = owner.__attributes__.get(self.foreign_key) | ||
return builder.where(self.local_key, foreign_key_value) | ||
|
||
|
||
def __call__(self, owner): | ||
"""Fetch the related record when invoked.""" | ||
related_model = self.model_class | ||
# print("related model", related_model) | ||
related_model.owner = self | ||
self.owner = owner | ||
foreign_key_value = owner.__attributes__.get(self.foreign_key) | ||
if not foreign_key_value: | ||
return None | ||
# builder = related_model.builder.where(self.local_key, foreign_key_value) | ||
builder = self.apply_query(related_model.builder) | ||
result = builder.get() | ||
for item in result: | ||
item.__dict__['related'] = self | ||
self.owner = owner | ||
return result | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from .BaseRelationship import BaseRelationship | ||
class HasOne(BaseRelationship): | ||
"""Belongs To Relationship Class.""" | ||
|
||
def __init__(self, model_class, foreign_key=None, local_key=None): | ||
self.model_class = model_class | ||
self.foreign_key = foreign_key | ||
self.local_key = local_key | ||
self.owner = None | ||
|
||
def apply_query(self, builder): | ||
owner = self.owner | ||
foreign_key_value = owner.__attributes__.get(self.foreign_key) | ||
builder = builder.where(self.local_key, foreign_key_value) | ||
return builder | ||
|
||
|
||
def __call__(self, owner): | ||
"""Fetch the related record when invoked.""" | ||
related_model = self.model_class | ||
# print("related model", related_model) | ||
related_model.owner = self | ||
self.owner = owner | ||
foreign_key_value = owner.__attributes__.get(self.foreign_key) | ||
if not foreign_key_value: | ||
return None | ||
# builder = related_model.builder.where(self.local_key, foreign_key_value) | ||
builder = self.apply_query(related_model.builder) | ||
result = builder.first() | ||
self.owner = owner | ||
result.__dict__['related'] = self | ||
return result | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .BaseRelationship import BaseRelationship | ||
from .HasOne import HasOne | ||
from .HasMany import HasMany |