1

Related to this answer on how to check for the (non)existance of related entities using Doctrine's is empty query, I try to use this within a Doctrine Expression, but the Expr Class has no documented way to use is empty. isNull doesn't work. So what I am looking for is an Expression like:

// filter for e with no relatedEntities
$qb->expr()->isEmpty('e.realatedEntities');
// filter for e with relatedEntities
$qb->expr()->isNotEmpty('e.realatedEntities');

So any ideas on this?

Doctrine's SIZE also has also no equivalent in the Expr-Class. Am I missing something? For now, I manipulate the query object instead of returning an expression (in Lexik form filter bindle for Symfony), but this is only a workaround.

spackmat
  • 892
  • 9
  • 23

1 Answers1

3

As the operands of the comparison expressions are DQL expressions themselves you can write:

$qb->expr()->gt('size(e.relatedEntities)', 0)

They get converted to a DQL string in the expression's __toString() method which in this case is defined as:

$this->leftExpr . ' ' . $this->operator . ' ' . $this->rightExpr;

So this should produce:

"size(e.relatiedEntities) > 0"

Further reading:

As $qb->expr() does nothing more than return Doctrine\ORM\Query\Expr objects you can take a look into the classes of that namespace if you want some information about building more sophisticated expressions.

yceruto
  • 9,230
  • 5
  • 38
  • 65
flu
  • 14,307
  • 8
  • 74
  • 71