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:
parent
4eab0670c4
commit
8cb57cf353
|
|
@ -385,6 +385,11 @@ class Lists extends WidgetBase
|
|||
*/
|
||||
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))) {
|
||||
continue;
|
||||
}
|
||||
|
|
@ -493,6 +498,11 @@ class Lists extends WidgetBase
|
|||
: $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);
|
||||
}
|
||||
|
||||
|
|
@ -506,7 +516,7 @@ class Lists extends WidgetBase
|
|||
/*
|
||||
* Add custom selects
|
||||
*/
|
||||
$query->select($selects);
|
||||
$query->addSelect($selects);
|
||||
|
||||
/*
|
||||
* Extensibility
|
||||
|
|
@ -840,8 +850,10 @@ class Lists extends WidgetBase
|
|||
else {
|
||||
if ($record->hasRelation($columnName) && array_key_exists($columnName, $record->attributes)) {
|
||||
$value = $record->attributes[$columnName];
|
||||
}
|
||||
else {
|
||||
// Load the value from the relationship counter if useRelationCount is specified
|
||||
} elseif ($column->relation && @$column->config['useRelationCount']) {
|
||||
$value = $record->{"{$column->relation}_count"};
|
||||
} else {
|
||||
$value = $record->{$columnName};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue