levanlau
(Vanlau Le)
August 2, 2014, 2:20pm
1
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
evstevemd
(Stefano Mtangoo)
August 2, 2014, 8:24pm
2
This was discussed and here is a link.
I have not tried it though
Currently in order to make a search function (not following gii approach) a user needs to do the following:
$query = Contact::find();
if(!empty($this->full_name))
{
...
severity:critical
status:under development
levanlau
(Vanlau Le)
August 3, 2014, 6:21am
3
I also skim that post and tried but not result.thanks
tuga
(Tuga74)
August 3, 2014, 4:20pm
4
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.
evstevemd
(Stefano Mtangoo)
August 13, 2014, 2:16pm
6
what do you want to achieve?
levanlau
(Vanlau Le)
August 14, 2014, 9:17am
7
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.
evstevemd
(Stefano Mtangoo)
August 15, 2014, 6:48am
8
suppose your model is Product, something like this should work (not tested yet)
Product::find()
->where(['shop'=>'s1'])
->andWhere([name=>'moible', 'keywork'=>'mobile'])
->all();
levanlau
(Vanlau Le)
August 16, 2014, 12:41am
9
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.
levanlau
(Vanlau Le)
August 17, 2014, 2:43pm
11
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’))?
levanlau
(Vanlau Le)
August 18, 2014, 3:20pm
12
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!