1

I am attempting to use PostgreSQL's NOT SIMILAR TO exclude a blacklist from the results of a query,

When I run the query in my repository method below:

$qb = $this->getEntityManager()->createQueryBuilder('p');

$query = $qb
    ->select('p')
    ->from('CRMPiccoBundle:Person', 'p')
    ->where("lower(p.email) not similar to '(" . implode('|', $blacklist) . ")%'")
    ->getQuery();

return $query->getResult();

I get the following error:

[Doctrine\ORM\Query\QueryException]                                                                                                                
SELECT p FROM CRMPiccoBundle:Person p WHERE lower(p.email) not similar to '(abuse@|admin@|billing@|compliance@|devnull@)%' 

[Doctrine\ORM\Query\QueryException]                                     
[Syntax Error] line 0, col 94: Error: Expected end of string, got 'to'  

However, when I run this query against my local DB with PgAdmin it works.

How can I achieve this with Doctrine using the Symfony Doctrine Query builder (or similar)? I am using PostgreSQL 9.5.5

2 Answers 2

1
$qb = $this->getEntityManager()->createQueryBuilder('p');

$select = $qb
    ->select('p')
    ->from('CRMPiccoBundle:Person', 'p')
;

foreach ($blacklist as $key => $item) {
    $select
        ->where('lower(p.email) NOT LIKE :key'.$key)
        ->setParameter('key'.$key, "$item%")
    ;
}

$query = $select->getQuery();

return $query->getResult();
Sign up to request clarification or add additional context in comments.

2 Comments

@crmpicco thanks for the edit, I don't keep an eye on that, because I only write the code without testing :D
Thanks for the answer, I didn't think about lining up the NOT LIKEs like that. It makes perfect sense when I seen your answer.
0

martin-georgiev/postgresql-for-doctrine @ GitHub adds support for REGEXP, SIMILAR_TO, NOT_SIMILAR_TO, JSON_GET_FIELD and CONTAINS operators, among others.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.