2

我想使用不区分重音来搜索单词。对于不区分大小写,我使用ilike

$query->andFilterWhere(['ilike', 'name', $this->name]);

但是对于口音不敏感,我不知道 Yii2 解决方案(否则我可以使用这个 PHP解决方案)。

在下一个示例中,我搜索了“camara”这个词,但没有找到“cámara”这个词(在西班牙语中是相机的意思):

未找到结果

4

2 回答 2

0

我花了一些时间来完成这项工作,@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 ILKE 的示例):

$query->andFilterWhere(self::ExpressionLikeAccentInsensitive('"DB_COLUMN"', $this->DB_COLUMN));
于 2020-10-20T19:26:14.807 回答
0

我使用lower_unaccent PostgreSQL 函数ILIKE PostgreSQL 运算符解决了它:

$query->andWhere(new Expression(
    'lower_unaccent(name) ILIKE \'%\' || lower_unaccent(\'' . $this->name . '\') || \'%\''
));
于 2018-12-12T17:58:39.903 回答