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
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);
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'
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With