Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Select fields with JSON_SEARCH to filter multiple values #2995

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Storage/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,14 @@ private function getRegularFieldExpression(Filter $filter): string
private function getRegularFieldWhereExpression(Filter $filter, string $valueAlias): string
{
if ($this->utils->isFieldType($this, $filter->getKey(), SelectField::TYPE) && $this->utils->hasJsonSearch()) {
// todo: Instead of using only the 1st param, make sure that the whole expression works.
// this is the case for things like multiselect: abc || def
return sprintf("JSON_SEARCH(%s, 'one', :%s) != ''", $valueAlias, key($filter->getParameters()));
$expressions = preg_split('/\s?(AND|OR)\s?/', $filter->getExpression());

$newExpressions = array_map(function($expression) use ($valueAlias) {
preg_match('/:\w+/', $expression, $parameter);
return sprintf("JSON_SEARCH(%s, 'one', %s) != ''", $valueAlias, $parameter[0]);
}, $expressions);

return str_replace($expressions, $newExpressions, $filter->getExpression());
}

$originalLeftExpression = 'content.' . $filter->getKey();
Expand Down