Skip to content
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
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion modules/backend/behaviors/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Copy link
Member

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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.


if (!empty($inverseRelation)){
$this->relationModel->{$inverseRelation} = $this->model;
}

$this->manageId = post('manage_id');
$this->foreignId = post('foreign_id');
$this->readOnly = $this->getConfig('readOnly');
Expand Down Expand Up @@ -657,7 +674,6 @@ protected function makeSearchWidget()
protected function makeViewWidget()
{
$widget = null;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to have whitespace added back in

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't change anything at 660...
Only 362 to 382.

         /*
         * Parent relationship
         */

        $parentClass = get_class($this->model);
        $allRelationDefinitions = $this->relationModel->getRelationDefinitions();
        $parentClassName = "";

        foreach ($allRelationDefinitions as $relations) {
            foreach ($relations as $key => $value) {
                if (strtolower($value[0]) == strtolower($parentClass)) {
                    $parentClassName = $key;
                } 

            } 

        } 

        if ($parentClassName != "") {
              $this->relationModel->{$parentClassName} = $this->model;
        }

/*
* Multiple (has many, belongs to many)
*/
Expand Down
Loading