我想使用口音不敏感的搜索单词。对于不区分大小写的情况,我使用ilike
$query->andFilterWhere(['ilike', 'name', $this->name]);但是由于口音不敏感,我不知道Yii2解决方案(否则我可以使用这个solution)。
在下一个例子中,我搜索了"camara“这个词,但是它找不到"cámara”这个词(在西班牙语中表示相机):

发布于 2018-12-12 17:58:39
我使用lower_unaccent PostgreSQL函数和ILIKE运算符解析它。
$query->andWhere(new Expression(
'lower_unaccent(name) ILIKE \'%\' || lower_unaccent(\'' . $this->name . '\') || \'%\''
));发布于 2020-10-20 19:26:14
我花了一段时间使用@rob006有关sql注入的评论来完成这项工作:
private static function ExpressionLikeAccentInsensitive($col, $field){
$bind = md5($col);
return [
'ilike',
new Expression("lower(unaccent({$col}))"),
new Expression("'%' || lower(unaccent(:q{$bind})) || '%'", [
":q{$bind}"=>$field
]),
];
}然后您这样称呼它(例如使用Postgres ):
$query->andFilterWhere(self::ExpressionLikeAccentInsensitive('"DB_COLUMN"', $this->DB_COLUMN));https://stackoverflow.com/questions/53745135
复制相似问题