fix: [component:CRUD] Extraction contextual filters based on association type

pull/37/head
mokaddem 2021-01-14 16:34:21 +01:00
parent 65252a39ff
commit 227816fe88
1 changed files with 13 additions and 3 deletions

View File

@ -501,9 +501,19 @@ class CRUDComponent extends Component
if (count($exploded) > 1) {
$model = $exploded[0];
$subField = $exploded[1];
$fieldToExtract = sprintf('%s.%s', Inflector::singularize(strtolower($model)), $subField);
$query = $this->Table->find()->contain($model)->distinct($field);
return $query->all()->extract($fieldToExtract)->toList();
$associationType = $this->Table->associations()->get($model)->type();
if ($associationType == 'oneToMany' || $associationType == 'manyToMany') {
$fieldToExtract = sprintf('%s.{*}.%s', strtolower($model), $subField);
} else {
$fieldToExtract = sprintf('%s.%s', Inflector::singularize(strtolower($model)), $subField);
}
$query = $this->Table->find()->contain($model);
return $query->all()->extract($fieldToExtract)->reduce(function ($output, $value) {
if (!in_array($value, $output)) {
$output[] = $value;
}
return $output;
}, []);
} else {
return $this->Table->find()->distinct([$field])->all()->extract($field)->toList();
}