How to use an IN statement with a Gii request for a Yii2REST web service

The Yii2 REST Web Services documentation explains that we can filter the found collection using the request parameters passed through the URL in the GET HTTP request.

From the document: "In addition, you can sort collections, such as http: // localhost / users? Sort = email or http: // localhost / users? Sort = -email . Filtering collections , for example http: // localhost / users ? filter [id] = 10 or http: // localhost / users? filter [email] [like] = gmail.com can be implemented using data filters "

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

The IN clause is supported by the framework data filter class, but it does not work as I do it. I tried:

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

... and other ways of doing the same thing

+3
source share
2 answers

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

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

Becouse Yii converts it to an IN request, see example

NB: Without specifying the [in] operator in the URI, otherwise it will not work

0
source

, DataFilter, , .

.. , usecase , , $dataFilter->load(Yii::$app->request->queryParams)

, ,

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

docs

+1

Source: https://habr.com/ru/post/1688936/


All Articles