chg: [CRUDComponent] Improved related filter condition
parent
00528112f1
commit
5d1106e82a
|
@ -373,29 +373,50 @@ class CRUDComponent extends Component
|
||||||
if (is_array($filterValue)) {
|
if (is_array($filterValue)) {
|
||||||
$query->where([($filter . ' IN') => $filterValue]);
|
$query->where([($filter . ' IN') => $filterValue]);
|
||||||
} else {
|
} else {
|
||||||
if (strlen(trim($filterValue, '%')) === strlen($filterValue)) {
|
$query = $this->setValueCondition($query, $filter, $filterValue);
|
||||||
$query->where([$filter => $filterValue]);
|
|
||||||
} else {
|
|
||||||
$query->like([$filter => $filterValue]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!empty($params['relatedFilters'])) {
|
if (!empty($params['relatedFilters'])) {
|
||||||
foreach ($params['relatedFilters'] as $filter => $filterValue) {
|
foreach ($params['relatedFilters'] as $filter => $filterValue) {
|
||||||
$filterParts = explode('.', $filter);
|
$filterParts = explode('.', $filter);
|
||||||
$query->matching($filterParts[0], function(\Cake\ORM\Query $q) use ($filterValue, $filter) {
|
$query = $this->setNestedRelatedCondition($query, $filterParts, $filterValue);
|
||||||
if (strlen(trim($filterValue, '%')) === strlen($filterValue)) {
|
|
||||||
return $q->where([$filter => $filterValue]);
|
|
||||||
} else {
|
|
||||||
return $q->like([$filter => $filterValue]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function setNestedRelatedCondition($query, $filterParts, $filterValue)
|
||||||
|
{
|
||||||
|
$modelName = $filterParts[0];
|
||||||
|
if (count($filterParts) == 2) {
|
||||||
|
$fieldName = implode('.', $filterParts);
|
||||||
|
$query = $this->setRelatedCondition($query, $modelName, $fieldName, $filterValue);
|
||||||
|
} else {
|
||||||
|
$filterParts = array_slice($filterParts, 1);
|
||||||
|
$query = $query->matching($modelName, function(\Cake\ORM\Query $q) use ($filterParts, $filterValue) {
|
||||||
|
return $this->setNestedRelatedCondition($q, $filterParts, $filterValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setRelatedCondition($query, $modelName, $fieldName, $filterValue)
|
||||||
|
{
|
||||||
|
return $query->matching($modelName, function(\Cake\ORM\Query $q) use ($fieldName, $filterValue) {
|
||||||
|
return $this->setValueCondition($q, $fieldName, $filterValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setValueCondition($query, $fieldName, $value)
|
||||||
|
{
|
||||||
|
if (strlen(trim($value, '%')) === strlen($value)) {
|
||||||
|
return $query->where([$fieldName => $value]);
|
||||||
|
} else {
|
||||||
|
return $query->like([$fieldName => $value]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected function setFilteringContext($contextFilters, $params)
|
protected function setFilteringContext($contextFilters, $params)
|
||||||
{
|
{
|
||||||
$filteringContexts = [];
|
$filteringContexts = [];
|
||||||
|
|
Loading…
Reference in New Issue