Allowing string for comparison operator >,<,>=,<=
DataFilter doesn't allow string for comparison operator >,<,>=,<= by default. However mysql, mssql and php all allows string comparisons. ex : 'ball' < 'water' is true Shouldn't DataFilter allow it too?
class DataFilter extends Model
{
public $operatorTypes = [
'<' => [self::TYPE_INTEGER, self::TYPE_FLOAT, self::TYPE_DATETIME, self::TYPE_DATE, self::TYPE_TIME,],
'>' => [self::TYPE_INTEGER, self::TYPE_FLOAT, self::TYPE_DATETIME, self::TYPE_DATE, self::TYPE_TIME],
'<=' => [self::TYPE_INTEGER, self::TYPE_FLOAT, self::TYPE_DATETIME, self::TYPE_DATE, self::TYPE_TIME],
'>=' => [self::TYPE_INTEGER, self::TYPE_FLOAT, self::TYPE_DATETIME, self::TYPE_DATE, self::TYPE_TIME],
'=' => '*',
'!=' => '*',
'IN' => '*',
'NOT IN' => '*',
'LIKE' => [self::TYPE_STRING],
];
public function init()
{
// Manually adding string to comparison operators
$this->operatorTypes['<'][] = self::TYPE_STRING;
$this->operatorTypes['>'][] = self::TYPE_STRING;
$this->operatorTypes['<='][] = self::TYPE_STRING;
$this->operatorTypes['>='][] = self::TYPE_STRING;
parent::init();
}
What steps will reproduce the problem?
What is the expected result?
What do you get instead?
[{"field":"filter","message":"\"someString\" does not support operator \"lt\"."}]
Additional info
| Q | A |
|---|---|
| Yii version | 2.0.14 |
| PHP version | |
| Operating system |
I think it is a db dependent problem, and possible implementation of it requires configuring DataFilter before using it, for example, by reading the app configuration database connection (can be few, which to choose?), then inject the properer DataFilterDriver (or something like that) particular for each database containing a set of allowed operators.
Better doing it on db side by a function. Nevertheless I can be wrong :)
Indeed, I was assuming it might be a db dependent feature. But to my knowledge, most modern db and programming languages allows string comparison, >,<,<= and >=.
It is like that because mostly these operands do not make sense with strings if we're talking about data filtering. Property is public so if you need it you can always configure it.
It is like that because mostly these operands do not make sense with strings if we're talking about data filtering.
It makes perfect sense. If I want to find records starting from o to z all I need is to use >=o. I've used this feature multiple times in CRUDs.
OK. Let's fix it then.
This is now part of https://github.com/yiisoft/data package with this feature implemented.
The package is currently not used. Need to implement bindins to it.