Doctrine Query Builder, how to escape single quote?

Viewed 332

I am trying to run a replace query, like this:

$qb = $this->createQueryBuilder('q');

$qb->where("REPLACE(q.name,'-','') = :q")->setParameter('q', $q);

return $qb->getQuery()->execute();

This will work fine.

The problem is I try to replace a single quote "REPLACE(q.name,'\'','') = :q"

It will complain it cannot escape it.

same with "REPLACE(q.name,'\\'','') = :q" or "REPLACE(q.name,\"'\",'') = :q"

Any ideas?

1 Answers

Why don't you add a second parameter? Since REPLACE not exists in Doctrine by default, i think you have add the beberlei/DoctrineExtensions package. With this package the following will work.

$qb->where("REPLACE(q.name,:s,'') = :q")
    ->setParameter('q', $q)
    ->setParameter('s', "'");
Related