chg: [CRUD] allow for sorting on related model fields

- some hacks to resolve issues with sorting on related fields
pull/163/head
iglocska 2023-05-25 16:11:21 +02:00
parent abd5dab605
commit 7a8eb6ba50
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
1 changed files with 14 additions and 2 deletions

View File

@ -50,7 +50,6 @@ class CRUDComponent extends Component
if ($this->taggingSupported()) { if ($this->taggingSupported()) {
$options['filters'][] = 'filteringTags'; $options['filters'][] = 'filteringTags';
} }
$optionFilters = []; $optionFilters = [];
$optionFilters += empty($options['filters']) ? [] : $options['filters']; $optionFilters += empty($options['filters']) ? [] : $options['filters'];
foreach ($optionFilters as $i => $filter) { foreach ($optionFilters as $i => $filter) {
@ -85,9 +84,22 @@ class CRUDComponent extends Component
$this->Controller->paginate['order'] = $options['order']; $this->Controller->paginate['order'] = $options['order'];
} }
} }
if (!empty($this->request->getQuery('sort'))) {
$sort = $this->request->getQuery('sort');
$direction = $this->request->getQuery('direction');
if ($this->_validOrderFields($sort) && ($direction === 'asc' || $direction === 'desc')) {
$sort = explode('.', $sort);
if (count($sort) > 1) {
$sort[0] = Inflector::camelize(Inflector::pluralize($sort[0]));
}
$sort = implode('.', $sort);
$query->order($sort . ' ' . $direction);
}
}
if ($this->metaFieldsSupported() && !$this->Controller->ParamHandler->isRest()) { if ($this->metaFieldsSupported() && !$this->Controller->ParamHandler->isRest()) {
$query = $this->includeRequestedMetaFields($query); $query = $this->includeRequestedMetaFields($query);
} }
if (!$this->Controller->ParamHandler->isRest()) { if (!$this->Controller->ParamHandler->isRest()) {
$this->setRequestedEntryAmount(); $this->setRequestedEntryAmount();
} }
@ -1673,7 +1685,7 @@ class CRUDComponent extends Component
return false; return false;
} }
} else { } else {
$association = $this->Table->associations()->get($model); $association = $this->Table->associations()->get(Inflector::camelize(Inflector::pluralize($model)));
$associatedTable = $association->getTarget(); $associatedTable = $association->getTarget();
if (empty($associatedTable->getSchema()->typeMap()[$subField])) { if (empty($associatedTable->getSchema()->typeMap()[$subField])) {
return false; return false;