From 8ee7d3f5ae0e56b1e332ad18a8e6162365e4e4d7 Mon Sep 17 00:00:00 2001 From: Priit Perna Date: Wed, 17 May 2017 13:00:40 +0300 Subject: [PATCH] Added scope support to the Relation formwidget --- modules/backend/formwidgets/Relation.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/modules/backend/formwidgets/Relation.php b/modules/backend/formwidgets/Relation.php index 6397c7afe..c5f4699e5 100644 --- a/modules/backend/formwidgets/Relation.php +++ b/modules/backend/formwidgets/Relation.php @@ -43,6 +43,11 @@ class Relation extends FormWidgetBase */ public $emptyOption; + /** + * @var string Use a custom scope method for the list query. + */ + public $scope; + // // Object properties // @@ -66,6 +71,7 @@ class Relation extends FormWidgetBase 'nameFrom', 'descriptionFrom', 'emptyOption', + 'scope', ]); if (isset($this->config->select)) { @@ -118,6 +124,10 @@ class Relation extends FormWidgetBase $query->where($relationModel->getKeyName(), '<>', $model->getKey()); } + if ($scopeMethod = $this->scope) { + $query->$scopeMethod($model); + } + // 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 = []; @@ -139,6 +149,7 @@ class Relation extends FormWidgetBase $result = $query->getQuery()->get(); } + // Some simpler relations can specify a custom local or foreign "other" key, // which can be detected and implemented here automagically. $primaryKeyName = in_array($relationType, ['hasMany', 'belongsTo', 'hasOne'])