is it possible to make a search with Doctrine case insensitive?
Asked
Active
Viewed 2.3k times
4 Answers
32
This depends mainly on your Database-Server. A LIKE with MySQL is case insensitive a like with PostgreSQL is case sensitive. But you can help yourself with something like this:
$pattern = strtolower('HEllO WorlD');
$q = Doctrine_Query::create()
->select('u.username')
->from('User u')
->where("LOWER(u.username) LIKE ?", $pattern);

Abraham
- 8,525
- 5
- 47
- 53

Timo Haberkern
- 4,409
- 2
- 27
- 41
-
1And what about indexes on username column? – Jekis Mar 11 '16 at 07:07
7
Also, you can try:
$queryBuilder->where('LOWER(b.title) LIKE LOWER(:query)')
->setParameter('query', '%' . $query . '%');
Important: After converting a string that contains special characters to lower case with strtolower(), the special characters don’t appear correct.

Osoian Marcel
- 345
- 3
- 14
-
1You can prevent the PHP `strtolower` from breaking with using the `mb_strtolower` function. Of course PHP needs to contain the mbstring module for this to work. Although, I do not know if the database is quicker with `LOWER` or if PHP is at this point. – Euli Jan 18 '19 at 09:14
1
Other thing you can do is:
$qb->andWhere($qb->expr()->like('lower(o.name)', ':name'));
$qb->setParameter('name', '%'. strtolower($search) . '%');
Regards.

Nicolas Finelli
- 2,170
- 1
- 14
- 9
0
The best way to do this is to have a canonized username column, in that column you can store the lowercase version of the username column and any other canonization process you want to do and search and index that column instead of the username column, then just do
->where("u.canonic_username) LIKE ?", $pattern);
That way you can use indexes and all of that.

Franklin Rivero
- 581
- 1
- 3
- 18