Yii2 Active Record WHERE FIND_IN_SET

Hi,

in my case categories are stored for each post like: 1,3,5,6 and i need to extract post by one of id.

Is there a way to use FIND_IN_SET with WHERE without using findBySql in Active Record?

i need to get something like this (WHERE FIND_IN_SET(:category, categories)).

thx.

May be this will work for you, i assuming categorie is model.

$query=Categories::find()

->where(['post'=>[1,3,5,6]]) // you can pass here array also


->asArray()


->all();

Hi, thx for your answer.

Unfortunately this not working for me.

I need to get Posts by category id.

I have table:

id | name | categories


1 | test1 | 1,3

2 | test2 | 3

3 | test3 | 1,4

now, if i will use: $query = Posts::find()->where([‘categories’=>[3]])->asArray()->all(), i’ll get just 1 post:

2 | test2 | 3

but i need to get all posts with categories 3:

1 | test1 | 1,3

2 | test2 | 3

This will work,

Categories::find()->where(new Expression(‘FIND_IN_SET(:category_to_find, categories)’))->addParams([’:category_to_find’ => 3])->asArray()->all();