How do you create a where condition in Doctrine ODM to find only documents where two fields are equal?
A document exists with two int fields alpha
and beta
, I want to select all documents where these two fields are equal.
I've tried the following but it returns no results:
$qb = $this->createQueryBuilder();
$qb
->field('alpha')->in($alphaIds)
->where('this.alpha === this.beta')
->sort('id', 'DESC');
return $qb->getQuery()->execute();
This questions shows how to do it outside of Doctrine where alpha and beta are not equal https://stackoverflow.com/a/8433182/1283381
Use the Query Expressions in Doctrine\MongoDB\Query\Expr
class to create the where()
expression via the expr()
method as follows:
$qb = $this->createQueryBuilder();
$qb->addAnd(
$qb->expr()
->field('alpha')->in($alphaIds)
->where('this.alpha === this.beta')
)
->sort('id', 'DESC');
return $qb->getQuery()->execute();