如何在表达式中使用Doctrine的“空”?

时间:2017-01-30 12:25:02

标签: php symfony doctrine-orm

this answer on how to check for the (non)existance of related entities using Doctrine's is empty query相关,我尝试在Doctrine Expression中使用它,但Expr Class没有记录使用is empty的方法。 isNull不起作用。所以我要找的是一个表达式:

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

关于这个的任何想法?

Doctrine SIZEExpr - Class中也没有等效词。我错过了什么吗?现在,我操纵查询对象而不是返回一个表达式(在Lexik表格过滤器bindle for Symfony中),但这只是一种解决方法。

1 个答案:

答案 0 :(得分:2)

由于comparison expressions的操作数本身就是DQL表达式,你可以写:

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

它们在表达式__toString()方法中转换为DQL字符串,在本例中定义为:

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

所以这应该产生:

"size(e.relatiedEntities) > 0"

进一步阅读:

由于$qb->expr()只执行返回Doctrine\ORM\Query\Expr个对象,如果您需要有关构建更复杂表达式的一些信息,可以查看该命名空间的类。