Multi Like Operators On A Query

Hi everybody!

How to use multi LIKE operators on same a query?

I have a table with columns id,name,key_word,…

I’d like use LIKE operators to query to same input param named $keyWord,

$products = Product::find()->where([‘id’=>$id])

                ->andWhere(['LIKE','name',$keyWord])


                ->orWhere(['LIKE','key_word',$keyWord])


                ->all();

I have used query above but it doesn’t show result as my expecting,orWhere doesn’t combine with ->where([‘id’=>$id]) properly.

Anybody have any ideas ? Thanks

This was discussed and here is a link.

I have not tried it though

I also skim that post and tried but not result.thanks

Try


$products = Product::find()

->where(['and','id='.$id,['or',['like','name',$keyWord],['like','key_word',$keyWord]]])

->all();



I’ve not tested but should be something similar.

Hope it helps.

Still not result.

what do you want to achieve?

I have a Product table:

id | shop | name | keyword


1 | s1 | nokiaX | nokiax,mobile

2 | s2 | laptop-dell | laptop,dell

3 | s1 | samsung 256 | samsung

… | … | … | …

In view have a textbox search,as a user type and find a product with input condition are $shop + ($name or $keywork),$shop is shown in dropdown,texbox search can be $name or $keyword .This mean a user find all product which shop = s1, type into textbox search = ‘mobile’,the table will be found in both of columns “name” OR “keywork”.

=>where(shop=‘s1’ AND (name=‘moible’ OR keywork=‘mobile’)).

Thanks.

suppose your model is Product, something like this should work (not tested yet)


Product::find()

    	->where(['shop'=>'s1'])

    	->andWhere([name=>'moible', 'keywork'=>'mobile'])

    	->all();



I think I should have query result between ‘name’ and ‘keywork’ column is “OR” not is “AND” as andWhere([name=>‘moible’, ‘keywork’=>‘mobile’]).

If as your query,the result will be returned TRUE as the both of columns are TRUE,but i’d like one of them TRUE is enough,this mean it should be “OR” between ‘name’ and ‘keywork’ column.

There is orWhere()

Thank for your reply.

How will be the query string If the query condition is similar where(shop=‘s1’ AND (name LIKE ‘moible’ OR keywork LIKE ‘mobile’))?

Wow, I found out the answer for this:

$products = Product::find()->where([‘shop’=>$s])

                ->andWhere('name LIKE "%' . $keyWord . '%" ' .'OR key_word LIKE "%' . $keyWord . '%"')


                ->all();

Thanks everybody very much!