-
-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get parent model from its child relation #328
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a1284ec
Access relationship parent from its child relation
xyz1123581321 f5f1cd0
Access relationship parent from its child relation
xyz1123581321 9d82a04
Codestyles fixes
xyz1123581321 367331b
Comment fixes
xyz1123581321 19e9807
Codestyle fixes
xyz1123581321 228f8ba
Apply suggestions from code review
LukeTowers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -356,6 +356,23 @@ public function initRelation($model, $field = null) | |
$this->relationObject = $this->model->{$field}(); | ||
$this->relationModel = $this->relationObject->getRelated(); | ||
|
||
// Ensure that the other side of the relationship is set on the related model | ||
$parentClass = get_class($this->model); | ||
$allRelationDefinitions = $this->relationModel->getRelationDefinitions(); | ||
$inverseRelation = null; | ||
foreach ($allRelationDefinitions as $relations) { | ||
foreach ($relations as $key => $value) { | ||
if (strtolower($value[0]) === strtolower($parentClass)) { | ||
$inverseRelation = $key; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (!empty($inverseRelation)){ | ||
$this->relationModel->{$inverseRelation} = $this->model; | ||
} | ||
|
||
$this->manageId = post('manage_id'); | ||
$this->foreignId = post('foreign_id'); | ||
$this->readOnly = $this->getConfig('readOnly'); | ||
|
@@ -657,7 +674,6 @@ protected function makeSearchWidget() | |
protected function makeViewWidget() | ||
{ | ||
$widget = null; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs to have whitespace added back in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't change anything at 660...
|
||
/* | ||
* Multiple (has many, belongs to many) | ||
*/ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems a bit fragile to me, I hadn't realized the complexity of setting the inverse relationship. @bennothommo do you have any ideas for this?
Right now I'm concerned about how well it supports all of the relationship modes supported by the RelationController and also if the related model has multiple relationship definitions tying back to the original parent model, how would we support that / should we try to support that? I'm thinking our best bet for that issue is to explicitly detect any more than one possible inverse relationship and don't do anything if that's encountered. It's already a bit presumptuous to be setting the inverse relationship directly when it might have scopes or conditions limiting the current parent model from being set in the first place.
Or perhaps I'm just overthinking this all, @bennothommo any thoughts on the above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bennothommo ping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the extreme delay in responding to this one!
It is somewhat flimsy, but at the same time - conventionally speaking - we should be alright. I can't think of a better way right now, and even if I could, there's no telling whether it would work or not given our relation definition is completely different to Laravel's.