Yii Framework Forum: How to use IN operator with a GET request - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to use IN operator with a GET request

#1 User is offline   alvarofvr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 07-December 17
  • Location:Verona, Italy

Posted 07 December 2017 - 08:26 AM

The documentation of Yii2 REST Web Services explain that we can filter the searched collection through query params passed via URL in a GET HTTP request.

From doc: "Addionally, you can sort collections like localhost/users?sort=email or localhost/users?sort=-email. Filtering collections like localhost/users?filter[id]=10 or localhost/users?filter[email][like]=gmail.com could be implemented using data filters"

My question is how to use query params for an IN condition?

The IN condition is supported by data filter class of the framework but It does not work as I am doing it. I tried these:

  • localhost/api/v1/users?filter[id][in][]=1,2,3 (return empty response)
  • localhost/api/v1/users?filter[id][in]=[1,2,3] (return error message 'Operator "in" requires multiple operands.')


...and other ways same situation
0

#2 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,490
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 07 December 2017 - 10:12 AM

query params should map to yii/db/Query::where signature

// try this
localhost/api/v1/users?filter[id][]=1&filter[id][]=2&filter[id][]=3

0

#3 User is offline   alvarofvr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 07-December 17
  • Location:Verona, Italy

Posted 09 December 2017 - 10:04 AM

View Postalrazi, on 07 December 2017 - 10:12 AM, said:

query params should map to yii/db/Query::where signature

// try this
localhost/api/v1/users?filter[id][]=1&filter[id][]=2&filter[id][]=3



But you not use IN operator in the query params. Why?
0

#4 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,490
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 10 December 2017 - 10:52 AM

you don't need to explicitly use in operator if you doing a match against an array yii will convert it into an in query see the example below

http://www.yiiframew...tml#hash-format
0

#5 User is offline   alvarofvr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 07-December 17
  • Location:Verona, Italy

Posted Yesterday, 05:13 AM

View Postalrazi, on 10 December 2017 - 10:52 AM, said:

you don't need to explicitly use in operator if you doing a match against an array yii will convert it into an in query see the example below

http://www.yiiframew...tml#hash-format



OK but if the framework in the DataFilter class has the following operators why they are not used in this case?
    /**
     * @var array maps filter condition keywords to validation methods.
     * These methods are used by [[validateCondition()]] to validate raw filter conditions.
     */
    public $conditionValidators = [
        'AND' => 'validateConjunctionCondition',
        'OR' => 'validateConjunctionCondition',
        'NOT' => 'validateBlockCondition',
        '<' => 'validateOperatorCondition',
        '>' => 'validateOperatorCondition',
        '<=' => 'validateOperatorCondition',
        '>=' => 'validateOperatorCondition',
        '=' => 'validateOperatorCondition',
        '!=' => 'validateOperatorCondition',
        'IN' => 'validateOperatorCondition',
        'NOT IN' => 'validateOperatorCondition',
        'LIKE' => 'validateOperatorCondition',
    ];

In each case thank you it work!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users