From a1284ec0532ec0a7575c75eda20b097e0bc2136a Mon Sep 17 00:00:00 2001 From: xyzqtc Date: Wed, 6 Oct 2021 12:12:38 +0200 Subject: [PATCH 1/6] Access relationship parent from its child relation If the parent relation is defined in its child relation, then assign its parent model to its child --- .../backend/behaviors/RelationController.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index c5972adaf4..6036d824b2 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -356,6 +356,27 @@ public function initRelation($model, $field = null) $this->relationObject = $this->model->{$field}(); $this->relationModel = $this->relationObject->getRelated(); + /* + * Relationship Parent + */ + + $parent_classname = ""; + + foreach($all_relations as $relations) + { + foreach($relations as $key => $value) + { + if (strtolower($value[0]) == strtolower($parent_class)) + { + $parent_classname = $key; + } + } + } + + if ($parent_classname != "") + $this->relationModel->{$parent_classname} = $this->model; + + $this->manageId = post('manage_id'); $this->foreignId = post('foreign_id'); $this->readOnly = $this->getConfig('readOnly'); From f5f1cd03c2a7677c6816f71efdfd4a4992b0b27f Mon Sep 17 00:00:00 2001 From: xyzqtc Date: Wed, 6 Oct 2021 12:46:57 +0200 Subject: [PATCH 2/6] Access relationship parent from its child relation If the parent relation is defined in its child relation, then assign its parent model to its child --- .../backend/behaviors/RelationController.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 6036d824b2..6ae0426dd4 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -360,21 +360,22 @@ public function initRelation($model, $field = null) * Relationship Parent */ - $parent_classname = ""; + $parentClass = get_class($this->model); + $allRelations = $this->relationModel->getRelationDefinitions(); - foreach($all_relations as $relations) + $parentClassName = ""; + + foreach($allRelations as $relations) { foreach($relations as $key => $value) { - if (strtolower($value[0]) == strtolower($parent_class)) - { - $parent_classname = $key; - } + if (strtolower($value[0]) == strtolower($parentClass)) + $parentClassName = $key; } } - if ($parent_classname != "") - $this->relationModel->{$parent_classname} = $this->model; + if ($parentClassName != "") + $this->relationModel->{$parentClassName} = $this->model; $this->manageId = post('manage_id'); From 9d82a04e1a78308920662d8f8bb08df90f96fcfd Mon Sep 17 00:00:00 2001 From: xyzqtc Date: Wed, 6 Oct 2021 20:59:30 +0200 Subject: [PATCH 3/6] Codestyles fixes --- modules/backend/behaviors/RelationController.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 6ae0426dd4..50ce28b85e 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -365,18 +365,19 @@ public function initRelation($model, $field = null) $parentClassName = ""; - foreach($allRelations as $relations) - { - foreach($relations as $key => $value) - { - if (strtolower($value[0]) == strtolower($parentClass)) + foreach($allRelations as $relations){ + + foreach($relations as $key => $value){ + if (strtolower($value[0]) == strtolower($parentClass)){ $parentClassName = $key; + } } + } - if ($parentClassName != "") + if ($parentClassName != ""){ $this->relationModel->{$parentClassName} = $this->model; - + } $this->manageId = post('manage_id'); $this->foreignId = post('foreign_id'); From 367331b0e16e5891c94afadcbc1db4987f934e6c Mon Sep 17 00:00:00 2001 From: xyzqtc Date: Wed, 6 Oct 2021 21:02:10 +0200 Subject: [PATCH 4/6] Comment fixes --- modules/backend/behaviors/RelationController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 50ce28b85e..b326c009be 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -366,13 +366,11 @@ public function initRelation($model, $field = null) $parentClassName = ""; foreach($allRelations as $relations){ - foreach($relations as $key => $value){ if (strtolower($value[0]) == strtolower($parentClass)){ $parentClassName = $key; } } - } if ($parentClassName != ""){ From 19e98073cbdeb872e8af0b4dea7f8c34e7f51cd1 Mon Sep 17 00:00:00 2001 From: xyzqtc Date: Thu, 7 Oct 2021 13:02:47 +0200 Subject: [PATCH 5/6] Codestyle fixes --- modules/backend/behaviors/RelationController.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index b326c009be..edbaefc339 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -215,6 +215,8 @@ class RelationController extends ControllerBehavior */ public $deferredBinding = false; + + /** * Behavior constructor * @param Backend\Classes\Controller $controller @@ -317,6 +319,7 @@ public function initRelation($model, $field = null) $this->config = $this->originalConfig; $this->model = $model; + $this->field = $field; if ($field == null) { @@ -357,20 +360,21 @@ public function initRelation($model, $field = null) $this->relationModel = $this->relationObject->getRelated(); /* - * Relationship Parent + * Parent relationship */ $parentClass = get_class($this->model); - $allRelations = $this->relationModel->getRelationDefinitions(); - + $allRelationDefinitions = $this->relationModel->getRelationDefinitions(); $parentClassName = ""; - foreach($allRelations as $relations){ - foreach($relations as $key => $value){ + foreach ($allRelationDefinitions as $relations){ + foreach ($relations as $key => $value){ if (strtolower($value[0]) == strtolower($parentClass)){ $parentClassName = $key; } + } + } if ($parentClassName != ""){ @@ -678,7 +682,6 @@ protected function makeSearchWidget() protected function makeViewWidget() { $widget = null; - /* * Multiple (has many, belongs to many) */ @@ -1113,6 +1116,7 @@ public function onRelationManageCreate() $saveData = $this->manageWidget->getSaveData(); $sessionKey = $this->deferredBinding ? $this->relationGetSessionKey(true) : null; + if ($this->viewMode == 'multi') { $newModel = $this->relationModel; From 228f8ba7423aa481a6fa4e4c665e65a7c9b2f9e0 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Mon, 11 Oct 2021 14:53:37 -0600 Subject: [PATCH 6/6] Apply suggestions from code review --- .../backend/behaviors/RelationController.php | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index edbaefc339..7832883633 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -215,8 +215,6 @@ class RelationController extends ControllerBehavior */ public $deferredBinding = false; - - /** * Behavior constructor * @param Backend\Classes\Controller $controller @@ -319,7 +317,6 @@ public function initRelation($model, $field = null) $this->config = $this->originalConfig; $this->model = $model; - $this->field = $field; if ($field == null) { @@ -359,26 +356,21 @@ public function initRelation($model, $field = null) $this->relationObject = $this->model->{$field}(); $this->relationModel = $this->relationObject->getRelated(); - /* - * Parent relationship - */ - + // Ensure that the other side of the relationship is set on the related model $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; + $inverseRelation = null; + foreach ($allRelationDefinitions as $relations) { + foreach ($relations as $key => $value) { + if (strtolower($value[0]) === strtolower($parentClass)) { + $inverseRelation = $key; + break; } - } - } - if ($parentClassName != ""){ - $this->relationModel->{$parentClassName} = $this->model; + if (!empty($inverseRelation)){ + $this->relationModel->{$inverseRelation} = $this->model; } $this->manageId = post('manage_id'); @@ -1116,7 +1108,6 @@ public function onRelationManageCreate() $saveData = $this->manageWidget->getSaveData(); $sessionKey = $this->deferredBinding ? $this->relationGetSessionKey(true) : null; - if ($this->viewMode == 'multi') { $newModel = $this->relationModel;