L
L
lenkagruzd2018-10-01 12:23:50
Yii
lenkagruzd, 2018-10-01 12:23:50

(YII2) How to arrange AR search when searching for multiple values ​​of one parameter?

Good day.
It is required to solve a problem: in AR search, more than 1 value must be processed at a time to search by parameter.
There is a city column. Enter in the search: Kazan, Samara, Rostov, etc. through the separator. The search processes each of the cities and outputs matches.
In the Search model, it was not possible to organize the foreach iteration. We split the request through 'explode' by comma and sent it to the loop.

foreach ($array as $city_str) {
            $query->andFilterWhere(['like', 'city', $city_str]);

Tell me, in which direction to dig?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-10-01
@lenkagruzd

I understand that instead
it should be
, or most likely like this:

$query->andFilterWhere(['or',
['like', 'city', $city_str1],
['like', 'city', $city_str2],
....
['like', 'city', $city_str999],
]);

Because your option will find the city only if it is called something like this "Kazan-Samara-Rostovsk"

D
Decadal, 2018-10-01
@Decadal

->where(['city' => $cities])->all();
where $cities is a list of cities (array).
The problem arises as soon as a fuzzy search (by part of the value) is needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question