Hossam Aldeen Ahmed Hossam Aldeen Ahmed - 3 months ago 9
MySQL Question

Yii2 add like where with one side %

i want to add where condition in yii2 with

like
condition with one side
%

to be like

where name like 'value%'

Table::find()->filterWhere(['like', 'name' , $_GET['q'].'%' ])
->all();


that result query like

where name like '%value\%%'

Answer

You need set the third operand to false in order to use custom where like conditions:

Table::find()->where(['like', 'name', $_GET['q'] . '%', false]);

From the Docs:

Sometimes, you may want to add the percentage characters to the matching value by yourself, you may supply a third operand false to do so. For example, ['like', 'name', '%tester', false] will generate name LIKE '%tester'.

Comments