From 5f12d9fc9b78023c2877f7dc31d8735c280c0544 Mon Sep 17 00:00:00 2001 From: Sam Georges Date: Tue, 23 Sep 2014 07:55:40 +1000 Subject: [PATCH] Keep relation constraints in Relation form widget --- modules/backend/formwidgets/Relation.php | 50 ++++++++++++++---------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/modules/backend/formwidgets/Relation.php b/modules/backend/formwidgets/Relation.php index debdaf3dc..6a38dfbc2 100644 --- a/modules/backend/formwidgets/Relation.php +++ b/modules/backend/formwidgets/Relation.php @@ -3,6 +3,7 @@ use Lang; use Backend\Classes\FormWidgetBase; use System\Classes\SystemException; +use Illuminate\Database\Eloquent\Relations\Relation as RelationBase; /** * Form Relationship @@ -89,30 +90,39 @@ class Relation extends FormWidgetBase */ protected function makeRenderFormField() { - $field = clone $this->formField; - $relatedObj = $this->model->makeRelation($this->relationName); - $query = $relatedObj->newQuery(); + return $this->renderFormField = RelationBase::noConstraints(function() { - if (in_array($this->relationType, ['belongsToMany', 'morphToMany', 'morphedByMany'])) { - $field->type = 'checkboxlist'; - } - else if ($this->relationType == 'belongsTo') { - $field->type = 'dropdown'; - $field->placeholder = $this->emptyOption; - } + $field = clone $this->formField; - // It is safe to assume that if the model and related model are of - // the exact same class, then it cannot be related to itself - if ($this->model->exists && (get_class($this->model) == get_class($relatedObj))) { - $query->where($relatedObj->getKeyName(), '<>', $this->model->id); - } + list($model, $attribute) = $this->getModelArrayAttribute($this->relationName); + $relatedObj = $model->makeRelation($attribute); + $query = $model->{$attribute}()->newQuery(); - if (in_array('October\Rain\Database\Traits\NestedTree', class_uses($relatedObj))) - $field->options = $query->listsNested($this->nameFrom, $relatedObj->getKeyName()); - else - $field->options = $query->lists($this->nameFrom, $relatedObj->getKeyName()); + if (in_array($this->relationType, ['belongsToMany', 'morphToMany', 'morphedByMany'])) { + $field->type = 'checkboxlist'; + } + else if ($this->relationType == 'belongsTo') { + $field->type = 'dropdown'; + $field->placeholder = $this->emptyOption; + } - return $this->renderFormField = $field; + // It is safe to assume that if the model and related model are of + // the exact same class, then it cannot be related to itself + if ($model->exists && (get_class($model) == get_class($relatedObj))) { + $query->where($relatedObj->getKeyName(), '<>', $model->id); + } + + // Even though "no constraints" is applied, belongsToMany constrains the query + // by joining its pivot table. Remove all joins from the query. + $query->getQuery()->getQuery()->joins = []; + + if (in_array('October\Rain\Database\Traits\NestedTree', class_uses($relatedObj))) + $field->options = $query->listsNested($this->nameFrom, $relatedObj->getKeyName()); + else + $field->options = $query->lists($this->nameFrom, $relatedObj->getKeyName()); + + return $field; + }); } /**