yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

Enhance andFilterCompare to include ranges

Open Iliolou opened this issue 2 years ago • 7 comments

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;
         }

Iliolou avatar Mar 03 '23 09:03 Iliolou

Interesting.

  1. Adjust code.
  2. Add docs.
  3. Add CHANGELOG line.

Do you have some time for a pull request?

samdark avatar Mar 03 '23 12:03 samdark

Not until next week, sorry. But if you think it's useful, just test it and add it as your own.

Iliolou avatar Mar 03 '23 22:03 Iliolou

@yiisoft/reviewers what do you think?

samdark avatar Mar 05 '23 17:03 samdark

  • 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 " ?

lubosdz avatar Mar 29 '23 16:03 lubosdz

  1. 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.
  2. Yes, case " 1.123 - 1.456 " works as expected.
  3. My customers needed it, and are actually very happy about it. That's why I though I should submit it.
  4. Not handled by the enhancement, returns the same as before (only the <> 123 part is validated).

Iliolou avatar Apr 05 '23 21:04 Iliolou

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.

bizley avatar Apr 06 '23 06:04 bizley

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.

lubosdz avatar Apr 06 '23 10:04 lubosdz