yii-api icon indicating copy to clipboard operation
yii-api copied to clipboard

Allowing string for comparison operator >,<,>=,<=

Open patrick-yi-82 opened this issue 7 years ago • 7 comments

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

patrick-yi-82 avatar Apr 04 '18 04:04 patrick-yi-82

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 :)

itma avatar Apr 04 '18 10:04 itma

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 >=.

patrick-yi-82 avatar Apr 05 '18 16:04 patrick-yi-82

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.

samdark avatar Apr 05 '18 18:04 samdark

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.

rob006 avatar Apr 05 '18 19:04 rob006

OK. Let's fix it then.

samdark avatar Apr 05 '18 19:04 samdark

This is now part of https://github.com/yiisoft/data package with this feature implemented.

samdark avatar Sep 08 '20 15:09 samdark

The package is currently not used. Need to implement bindins to it.

samdark avatar Sep 08 '20 15:09 samdark