D
D
Dmitry Kolyada2018-05-01 12:21:43
Yii
Dmitry Kolyada, 2018-05-01 12:21:43

How to implement search for multiple values ​​in one field?

Hello everyone, please help me solve my problem. Let us have blogs. Blogs are associated with M:M tags. Accordingly, there is a table that lists all these tags (tags). How to display blogs, with a certain tag, this is clear to me, but how can I select all blogs in the SearchModel that have absolutely all available tags?
I have tried 2 options. Others do not climb into the head, it was not possible to google.
Option 1:

$tags = ArrayHelper::getColumn(Tag::find()->all(),'id'); // тут я получаю массив со всеми существующими тэгами
            $query->andFilterWhere([
                'service_id' => $tags
            ]);

Doesn't work because displays all blogs that have at least one of the tags.
Option 2:
$tags = ArrayHelper::getColumn(Tag::find()->all(),'id'); // тут я получаю массив со всеми существующими тэгами
            foreach ($tags as $one) {
                $query->andFilterWhere([
                    'service_id' => $one
                ]);
            }

it just doesn't work) I don't have any
other options, please have a look.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-05-01
@webinar

not very deeply penetrated, but firstly:
apparently, strict equality is needed because $this->service == 0)it is null as well.
Then you wrote some heresy, as I understand it, you need something like this:

if ($this->service !== 0){
       $query->andFilterWhere([ 'service_id' => $this->service]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question