Add useRelationCount column property

Adds the `useRelationCount` property to tell the list controller to use the number of related records for the specified `relation` as the value for that column.
This commit is contained in:
Luke Towers 2018-01-25 16:54:06 -06:00 committed by GitHub
parent 4eab0670c4
commit 8cb57cf353
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 8 deletions

View File

@ -385,6 +385,11 @@ class Lists extends WidgetBase
*/ */
foreach ($this->getVisibleColumns() as $column) { foreach ($this->getVisibleColumns() as $column) {
// If useRelationCount is enabled, eager load the count of the relation into $relation_count
if ($column->relation && @$column->config['useRelationCount']) {
$query->withCount($column->relation);
}
if (!$this->isColumnRelated($column) || (!isset($column->sqlSelect) && !isset($column->valueFrom))) { if (!$this->isColumnRelated($column) || (!isset($column->sqlSelect) && !isset($column->valueFrom))) {
continue; continue;
} }
@ -493,6 +498,11 @@ class Lists extends WidgetBase
: $column->valueFrom; : $column->valueFrom;
} }
// Set the sorting column to $relation_count if useRelationCount enabled
if (isset($column->relation) && @$column->config['useRelationCount']) {
$sortColumn = $column->relation . '_count';
}
$query->orderBy($sortColumn, $this->sortDirection); $query->orderBy($sortColumn, $this->sortDirection);
} }
@ -506,7 +516,7 @@ class Lists extends WidgetBase
/* /*
* Add custom selects * Add custom selects
*/ */
$query->select($selects); $query->addSelect($selects);
/* /*
* Extensibility * Extensibility
@ -840,20 +850,22 @@ class Lists extends WidgetBase
else { else {
if ($record->hasRelation($columnName) && array_key_exists($columnName, $record->attributes)) { if ($record->hasRelation($columnName) && array_key_exists($columnName, $record->attributes)) {
$value = $record->attributes[$columnName]; $value = $record->attributes[$columnName];
} // Load the value from the relationship counter if useRelationCount is specified
else { } elseif ($column->relation && @$column->config['useRelationCount']) {
$value = $record->{"{$column->relation}_count"};
} else {
$value = $record->{$columnName}; $value = $record->{$columnName};
} }
} }
/** /**
* @event backend.list.overrideColumnValueRaw * @event backend.list.overrideColumnValueRaw
* Overrides the raw column value in a list widget. * Overrides the raw column value in a list widget.
* *
* If a value is returned from this event, it will be used as the raw value for the provided column. * If a value is returned from this event, it will be used as the raw value for the provided column.
* `$value` is passed by reference so modifying the variable in place is also supported. Example usage: * `$value` is passed by reference so modifying the variable in place is also supported. Example usage:
* *
* Event::listen('backend.list.overrideColumnValueRaw', function($record, $column, &$value) { * Event::listen('backend.list.overrideColumnValueRaw', function($record, $column, &$value) {
* $value .= '-modified'; * $value .= '-modified';
* }); * });
* *
@ -895,12 +907,12 @@ class Lists extends WidgetBase
/** /**
* @event backend.list.overrideColumnValue * @event backend.list.overrideColumnValue
* Overrides the column value in a list widget. * Overrides the column value in a list widget.
* *
* If a value is returned from this event, it will be used as the value for the provided column. * If a value is returned from this event, it will be used as the value for the provided column.
* `$value` is passed by reference so modifying the variable in place is also supported. Example usage: * `$value` is passed by reference so modifying the variable in place is also supported. Example usage:
* *
* Event::listen('backend.list.overrideColumnValue', function($record, $column, &$value) { * Event::listen('backend.list.overrideColumnValue', function($record, $column, &$value) {
* $value .= '-modified'; * $value .= '-modified';
* }); * });
* *