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 2 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
22 changes: 22 additions & 0 deletions modules/backend/behaviors/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,28 @@ public function initRelation($model, $field = null)
$this->relationObject = $this->model->{$field}();
$this->relationModel = $this->relationObject->getRelated();

/*
* Relationship Parent
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
*/

$parentClass = get_class($this->model);
$allRelations = $this->relationModel->getRelationDefinitions();

$parentClassName = "";

foreach($allRelations as $relations)
{
foreach($relations as $key => $value)
{
if (strtolower($value[0]) == strtolower($parentClass))
$parentClassName = $key;
}
}
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 ($parentClassName != "")
$this->relationModel->{$parentClassName} = $this->model;
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved


$this->manageId = post('manage_id');
$this->foreignId = post('foreign_id');
$this->readOnly = $this->getConfig('readOnly');
Expand Down