Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i write not equal to in a doctrine where clause?

I hope someone can help me, i'm writing a custom query in my repository and i'd like to do the below:-

$query = $this->_em->createQueryBuilder()
->select('a')
->from('entity', 'a')
->where('a.deleted not 1') /// how do you write NOT??? i've tried <> etc 
->getQuery();

How do i perform the above?

Thanks

Andrew

like image 210
Andrew 'go-sql' Fenna Avatar asked May 10 '12 15:05

Andrew 'go-sql' Fenna


2 Answers

Francesco's answer is correct. It all depends on your will to use either the expression builder or a high level solution.

For your particular case you can choose one of the two.

Expression builder:

$queryBuilder = $this->_em->createQueryBuilder();
$expr = $queryBuilder->expr();

$query = $queryBuilder
    ->select('a')
    ->from('entity', 'a')
    ->where($expr->neq('a.deleted', 1))
    ->getQuery();

For high level solution see Rawkode's answer. Except changing the != to <> or using a.deleted = 0.

Better yet would be parametrizing this with Doctrine

->where('a.deleted = :deleted')
->setParameter('deleted', false);
like image 199
Krzysztof Karski Avatar answered Sep 28 '22 07:09

Krzysztof Karski


Just use neq() like in the following example:

$query = $repository->createQueryBuilder('t');
$expr = $query->expr();
$orx = $expr->orX();

$orx->add($expr->neq('t.pageTitle', $expr->literal('value1')));
$orx->add($expr->neq('t.metaDescription', $expr->literal('value2')));

$query->andWhere($orx);

The above code produces:

AND (
    t1_.page_title <> 'value1' 
    OR t1_.meta_description <> 'value2'
)
like image 42
Francesco Casula Avatar answered Sep 28 '22 06:09

Francesco Casula