Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IN operator with a GET request of Yii2REST web service

Tags:

php

yii2

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 http://localhost/users?sort=email or http://localhost/users?sort=-email. Filtering collections like http://localhost/users?filter[id]=10 or http://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:

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

...and other ways same situation

like image 646
alvaro fvr Avatar asked Nov 08 '17 12:11

alvaro fvr


2 Answers

until you post the code where you instantiate and load your DataFilter i'm not sure if this helps you.

.. so, based on the usecase in your question i'm assuming you're using
$dataFilter->load(Yii::$app->request->queryParams)

therefore this should be the proper way to have your query params formatted if you want them to evaluate corectly

?filter[id][in][]=1&filter[id][in][]=2&filter[id][in][]=3

based on these examples in the docs data filter and using data filters

like image 132
csminb Avatar answered Nov 03 '22 03:11

csminb


Hi guys it would seem that by specifying the parameter operator in GET there is no way to query with an IN condition so solution is this:

http://localhost/api/v1/users?filter[id][]=1&filter[id][]=2&filter[id][]=3

Becouse Yii will convert it into an IN query see the example

N.B.: Without specifying the [in] operator in URI otherwise it will not work

like image 1
alvaro fvr Avatar answered Nov 03 '22 03:11

alvaro fvr