I want to add like
condition with %
wildcard on the one side, like:
where name like 'value%'
My code:
Table::find()->filterWhere(['like', 'name' , $_GET['q'].'%' ])
->all();
But query result is:
where name like '%value\%%'
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 generatename LIKE '%tester'
.
You can use:
Table::find()->where(new \yii\db\Expression('name LIKE :term', [':term' => $_GET['q'] . '%']));
or
Table::find()->where(['like', 'name', $_GET['q'] . '%', false]);
or
$likeCondition = new \yii\db\conditions\LikeCondition('name', 'LIKE', $_GET['q'] . '%');
$likeCondition->setEscapingReplacements(false);
Table::find()->where($likeCondition);
More info at https://www.yiiframework.com/doc/api/2.0/yii-db-conditions-likecondition
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With