postgresqlyii2accent-insensitive

Yii2: accent insensitive filter


I want to search words using accent insensitive. For case insensitive I use ilike:

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

But for accent insensitive I don't know a Yii2 solution (else I can use this PHP solution).

In the next example I search the word "camara" but it doesn't find the word "cámara" (means camera in Spanish):

No results found


Solution

  • I resolved it using the lower_unaccent PostgreSQL function and the ILIKE PostgreSQL operator:

    $query->andWhere(new Expression(
        'lower_unaccent(name) ILIKE \'%\' || lower_unaccent(\'' . $this->name . '\') || \'%\''
    ));