Enhance andFilterCompare to include ranges
We can enhance andFilterCompare operator to include ranges .e.g 2-4 , apart from >3, <>3
if (preg_match('/^(<>|>=|>|<=|<|=)/', $value, $matches)) {
$operator = $matches[1];
$value = substr($value, strlen($operator));
+ } elseif (count($matches=preg_split("/-/",$value)) == 2) {
+ return $this->andFilterWhere(['between', $name, $matches[0], $matches[1]]);
} else {
$operator = $defaultOperator;
}
Interesting.
- Adjust code.
- Add docs.
- Add CHANGELOG line.
Do you have some time for a pull request?
Not until next week, sorry. But if you think it's useful, just test it and add it as your own.
@yiisoft/reviewers what do you think?
- should do much better type checks to prevent crash, e.g. " 5 - a ", " < 56-01", " 1 -", " - 987" , " 1.123 - 1.456 " ..
- floats supported too?
- seems like nice enhancement, but I don't remember needing it
- potential BC break e.g. " <> 123-456 " ?
- There are no crashes, it's just a filter. If it's invalid finds no records. Cases " < 56-01", " 1 -", " - 987" are not hanlded by my enhancement. First one starts with < and the rest do not have two arguments. Case " 5 - a " is valid and returns no records.
- Yes, case " 1.123 - 1.456 " works as expected.
- My customers needed it, and are actually very happy about it. That's why I though I should submit it.
- Not handled by the enhancement, returns the same as before (only the <> 123 part is validated).
Yes, I think we can add it, potential mismatches will be handled by the developer anyway since this is a filter. I'll ask for the same that Samdark did + tests.
At least numeric arguments should be ensured. Remember, there are multiple RDBMS supported and simply splitting by - and not checking numeric type may cause BC crashes for other users.