Skip to content
This repository has been archived by the owner on Dec 28, 2020. It is now read-only.

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

Open
patrick-yi-82 opened this issue Apr 4, 2018 · 7 comments
Open

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

patrick-yi-82 opened this issue Apr 4, 2018 · 7 comments
Labels
status:ready for adoption Feel free to implement this issue.

Comments

@patrick-yi-82
Copy link

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
@itma
Copy link

itma commented Apr 4, 2018

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

@patrick-yi-82
Copy link
Author

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

@samdark
Copy link
Member

samdark commented Apr 5, 2018

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 samdark closed this as completed Apr 5, 2018
@rob006
Copy link
Contributor

rob006 commented Apr 5, 2018

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.

@samdark samdark reopened this Apr 5, 2018
@samdark
Copy link
Member

samdark commented Apr 5, 2018

OK. Let's fix it then.

@samdark samdark transferred this issue from yiisoft/yii2 Apr 24, 2019
@samdark
Copy link
Member

samdark commented Sep 8, 2020

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

@samdark
Copy link
Member

samdark commented Sep 8, 2020

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

@samdark samdark added the status:ready for adoption Feel free to implement this issue. label Sep 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status:ready for adoption Feel free to implement this issue.
Projects
None yet
Development

No branches or pull requests

4 participants