Symfony2 Doctrine - ILIKE clause for PostgreSQL?
I don't know about Symphony, but you can substitute
a ILIKE b
with
lower(a) LIKE lower(b)
You could also try the operator ~~*
, which is a synonym for ILIKE
It has slightly lower operator precedence, so you might need parenthesis for concatenated strings where you wouldn't with ILIKE
a ILIKE b || c
becomes
a ~~* (b || c)
The manual about pattern matching, starting with LIKE
/ ILIKE
.
I think this guy had the same problem and got an answer:
http://forum.symfony-project.org/viewtopic.php?f=23&t=40424
Obviously, you can extend Symfony2 with SQL vendor specific functions:
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/cookbook/dql-user-defined-functions.html
I am not a fan of ORMs and frameworks butchering the rich functionality of Postgres just to stay "portable" (which hardly ever works).
This works for me (Symfony2 + Doctrine 2)
$qq = 'SELECT x FROM MyBundle:X x WHERE LOWER(x.y) LIKE :y';
$q = $em->createQuery($qq)->setParameter(':y', strtolower('%' . $filter . '%'));
$result = $q->getResult();
Unfortunately, we still do not have the ability to create custom comparison operators...
But we can create a custom function in which we implement the comparison.
Our DQL query well look like this:
SELECT q FROM App\Entity\Customer q WHERE ILIKE(q.name, :name) = true
The class describing the ILIKE function will look like this:
class ILike extends FunctionNode
{
/** @var Node */
protected $field;
/** @var Node */
protected $query;
/**
* @param Parser $parser
*
* @throws \Doctrine\ORM\Query\QueryException
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->field = $parser->StringExpression();
$parser->match(Lexer::T_COMMA);
$this->query = $parser->StringExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
/**
* @param SqlWalker $sqlWalker
*
* @return string
* @throws \Doctrine\ORM\Query\AST\ASTException
*/
public function getSql(SqlWalker $sqlWalker)
{
return '(' . $this->field->dispatch($sqlWalker) . ' ILIKE ' . $this->query->dispatch($sqlWalker) . ')';
}
}
The resulting SQL will look like this:
SELECT c0_.id AS id_0,
c0_.name AS name_1,
FROM customer c0_
WHERE (c0_.name ILIKE 'paramvalue') = true