new: [component:CRUD] Added support of IN condition when filtering index

refacto/CRUDComponent
Sami Mokaddem 2023-02-23 11:40:07 +01:00
parent 9bc1ba899f
commit fdd876b1b2
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
1 changed files with 23 additions and 1 deletions

View File

@ -10,6 +10,7 @@ use Cake\Utility\Text;
use Cake\View\ViewBuilder; use Cake\View\ViewBuilder;
use Cake\ORM\TableRegistry; use Cake\ORM\TableRegistry;
use Cake\ORM\Query; use Cake\ORM\Query;
use Cake\Database\Expression\QueryExpression;
use Cake\Routing\Router; use Cake\Routing\Router;
use Cake\Http\Exception\MethodNotAllowedException; use Cake\Http\Exception\MethodNotAllowedException;
use Cake\Http\Exception\NotFoundException; use Cake\Http\Exception\NotFoundException;
@ -1225,7 +1226,7 @@ class CRUDComponent extends Component
} }
$activeFilters[$filter] = $filterValue; $activeFilters[$filter] = $filterValue;
if (is_array($filterValue)) { if (is_array($filterValue)) {
$query->where([($filter . ' IN') => $filterValue]); $query = $this->setInCondition($query, $filter, $filterValue);
} else { } else {
$query = $this->setValueCondition($query, $filter, $filterValue); $query = $this->setValueCondition($query, $filter, $filterValue);
} }
@ -1311,6 +1312,27 @@ class CRUDComponent extends Component
} }
} }
protected function setInCondition($query, $fieldName, $values)
{
$split = explode(' ', $fieldName);
if (count($split) == 1) {
$field = $fieldName;
$operator = '=';
} else {
$field = $split[0];
$operator = $split[1];
}
if ($operator == '=') {
return $query->where(function (QueryExpression $exp, Query $q) use ($field, $values) {
return $exp->in($field, $values);
});
} else if ($operator == '!=') {
return $query->where(function (QueryExpression $exp, Query $q) use ($field, $values) {
return $exp->notIn($field, $values);
});
}
}
protected function setFilteringContext($contextFilters, $params) protected function setFilteringContext($contextFilters, $params)
{ {
$filteringContexts = []; $filteringContexts = [];