Skip to content

Commit 7fc3153

Browse files
committed
Fixed filter logic for exclusion of entities. Before parts with null values as property value were wrongly not shown
This fixes issue #658
1 parent 5231dbd commit 7fc3153

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/DataTables/Filters/Constraints/EntityConstraint.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function apply(QueryBuilder $queryBuilder): void
137137
}
138138

139139
//We need to handle null values differently, as they can not be compared with == or !=
140-
if (!$this->value instanceof AbstractDBElement) {
140+
if ($this->value === null) {
141141
if($this->operator === '=' || $this->operator === 'INCLUDING_CHILDREN') {
142142
$queryBuilder->andWhere(sprintf("%s IS NULL", $this->property));
143143
return;
@@ -152,8 +152,9 @@ public function apply(QueryBuilder $queryBuilder): void
152152
}
153153

154154
if($this->operator === '=' || $this->operator === '!=') {
155-
$this->addSimpleAndConstraint($queryBuilder, $this->property, $this->identifier, $this->operator, $this->value);
156-
return;
155+
//Include null values on != operator, so that really all values are returned that are not equal to the given value
156+
$this->addSimpleAndConstraint($queryBuilder, $this->property, $this->identifier, $this->operator, $this->value, $this->operator === '!=');
157+
return;
157158
}
158159

159160
//Otherwise retrieve the children list and apply the operator to it
@@ -168,7 +169,8 @@ public function apply(QueryBuilder $queryBuilder): void
168169
}
169170

170171
if ($this->operator === 'EXCLUDING_CHILDREN') {
171-
$this->addSimpleAndConstraint($queryBuilder, $this->property, $this->identifier, 'NOT IN', $list);
172+
//Include null values in the result, so that all elements that are not in the list are returned
173+
$this->addSimpleAndConstraint($queryBuilder, $this->property, $this->identifier, 'NOT IN', $list, true);
172174
return;
173175
}
174176
} else {

src/DataTables/Filters/Constraints/FilterTrait.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,25 @@ protected function generateParameterIdentifier(string $property): string
5656

5757
/**
5858
* Adds a simple constraint in the form of (property OPERATOR value) (e.g. "part.name = :name") to the given query builder.
59+
* @param QueryBuilder $queryBuilder The query builder to add the constraint to
60+
* @param string $property The property to compare
61+
* @param string $parameterIdentifier The identifier for the parameter
62+
* @param string $comparison_operator The comparison operator to use
63+
* @param mixed $value The value to compare to
64+
* @param bool $include_null If true, the result of this constraint will also include null values of this property (useful for exclusion filters)
5965
*/
60-
protected function addSimpleAndConstraint(QueryBuilder $queryBuilder, string $property, string $parameterIdentifier, string $comparison_operator, mixed $value): void
66+
protected function addSimpleAndConstraint(QueryBuilder $queryBuilder, string $property, string $parameterIdentifier, string $comparison_operator, mixed $value, bool $include_null = false): void
6167
{
6268
if ($comparison_operator === 'IN' || $comparison_operator === 'NOT IN') {
6369
$expression = sprintf("%s %s (:%s)", $property, $comparison_operator, $parameterIdentifier);
6470
} else {
6571
$expression = sprintf("%s %s :%s", $property, $comparison_operator, $parameterIdentifier);
6672
}
6773

74+
if ($include_null) {
75+
$expression = sprintf("(%s OR %s IS NULL)", $expression, $property);
76+
}
77+
6878
if($this->useHaving || $this->isAggregateFunctionString($property)) { //If the property is an aggregate function, we have to use the "having" instead of the "where"
6979
$queryBuilder->andHaving($expression);
7080
} else {

0 commit comments

Comments
 (0)