From 6685838308736ed9213b2e59fe943f6881cc9d9a Mon Sep 17 00:00:00 2001 From: mokaddem Date: Thu, 14 Jan 2021 15:30:16 +0100 Subject: [PATCH] fix: [component:CRUDComponent] Take data linked to the current table Performs a query similar to a right join instead of dumping the table for composed contextual filters --- src/Controller/Component/CRUDComponent.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index 47e3650..df02f06 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -394,8 +394,13 @@ class CRUDComponent extends Component foreach ($contextFilters['fields'] as $field) { $contextsFromField = $this->getFilteringContextFromField($field); foreach ($contextsFromField as $contextFromField) { + if (is_bool($contextFromField)) { + $contextFromFieldText = sprintf('%s: %s', $field, $contextFromField ? 'true' : 'false'); + } else { + $contextFromFieldText = $contextFromField; + } $filteringContexts[] = [ - 'label' => Inflector::humanize($contextFromField), + 'label' => Inflector::humanize($contextFromFieldText), 'filterCondition' => [ $field => $contextFromField ] @@ -496,9 +501,9 @@ class CRUDComponent extends Component if (count($exploded) > 1) { $model = $exploded[0]; $subField = $exploded[1]; - return $this->Table->{$model}->find() - ->distinct([$subField]) - ->extract($subField)->toList(); + $fieldToExtract = sprintf('%s.%s', Inflector::singularize(strtolower($model)), $subField); + $query = $this->Table->find()->contain($model)->distinct($field); + return $query->all()->extract($fieldToExtract)->toList(); } else { return $this->Table->find()->distinct([$field])->all()->extract($field)->toList(); }