Relation form field now supports model attributes in "nameFrom"
nameFromSelect -> select|sqlSelect Refs https://github.com/octobercms/october/pull/1579
This commit is contained in:
parent
7885074cb0
commit
030e1977e8
|
|
@ -1,11 +1,11 @@
|
|||
<?php namespace Backend\FormWidgets;
|
||||
|
||||
use Db;
|
||||
use Lang;
|
||||
use Backend\Classes\FormWidgetBase;
|
||||
use ApplicationException;
|
||||
use SystemException;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation as RelationBase;
|
||||
use DB;
|
||||
|
||||
/**
|
||||
* Form Relationship
|
||||
|
|
@ -31,9 +31,9 @@ class Relation extends FormWidgetBase
|
|||
public $descriptionFrom = 'description';
|
||||
|
||||
/**
|
||||
* @var string Model SQL column selection to use for the name reference
|
||||
* @var string Custom SQL column selection to use for the name reference
|
||||
*/
|
||||
public $nameFromSelect;
|
||||
public $sqlSelect;
|
||||
|
||||
/**
|
||||
* @var string Empty value to use if the relation is singluar (belongsTo)
|
||||
|
|
@ -63,8 +63,11 @@ class Relation extends FormWidgetBase
|
|||
'nameFrom',
|
||||
'descriptionFrom',
|
||||
'emptyOption',
|
||||
'nameFromSelect',
|
||||
]);
|
||||
|
||||
if (isset($this->config->select)) {
|
||||
$this->sqlSelect = $this->config->select;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -118,28 +121,26 @@ class Relation extends FormWidgetBase
|
|||
// by joining its pivot table. Remove all joins from the query.
|
||||
$query->getQuery()->getQuery()->joins = [];
|
||||
|
||||
/**
|
||||
* The nameFromSelect config takes precedence over nameFrom.
|
||||
* A virtual column called "selection" will contain the result.
|
||||
*/
|
||||
if ($this->nameFromSelect) {
|
||||
// Determine if the model uses a tree trait
|
||||
$treeTraits = ['October\Rain\Database\Traits\NestedTree', 'October\Rain\Database\Traits\SimpleTree'];
|
||||
$usesTree = count(array_intersect($treeTraits, class_uses($relationModel))) > 0;
|
||||
|
||||
// The "sqlSelect" config takes precedence over "nameFrom".
|
||||
// A virtual column called "selection" will contain the result.
|
||||
// Tree models must select all columns to return parent columns, etc.
|
||||
if ($this->sqlSelect) {
|
||||
$nameFrom = 'selection';
|
||||
$query = $query->select(
|
||||
DB::raw($this->nameFromSelect . ' AS ' . $nameFrom),
|
||||
$relationModel->getKeyName()
|
||||
);
|
||||
$selectColumn = $usesTree ? '*' : $relationModel->getKeyName();
|
||||
$result = $query->select($selectColumn, Db::raw($this->sqlSelect . ' AS ' . $nameFrom));
|
||||
}
|
||||
else {
|
||||
$nameFrom = $this->nameFrom;
|
||||
$result = $query->getQuery()->get();
|
||||
}
|
||||
|
||||
$treeTraits = ['October\Rain\Database\Traits\NestedTree', 'October\Rain\Database\Traits\SimpleTree'];
|
||||
if (count(array_intersect($treeTraits, class_uses($relationModel))) > 0) {
|
||||
$field->options = $query->listsNested($nameFrom, $relationModel->getKeyName());
|
||||
}
|
||||
else {
|
||||
$field->options = $query->lists($nameFrom, $relationModel->getKeyName());
|
||||
}
|
||||
$field->options = $usesTree
|
||||
? $result->listsNested($nameFrom, $relationModel->getKeyName())
|
||||
: $result->lists($nameFrom, $relationModel->getKeyName());
|
||||
|
||||
return $field;
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue