Doctrine\ORM\Query\QueryException [Semantical Error] ...: Error: Class 'NULL' is not defined. when using NULL in QueryBuilder

Doctrine\ORM\Query\QueryException [Semantical Error] ...: Error: Class 'NULL' is not defined. when using NULL in QueryBuilder

$result = $this->er->createQueryBuilder("r")
            ->select("COUNT(r.customer IS NOT NULL) as customers")
            ->getQuery()
            ->getResult(AbstractQuery::HYDRATE_ARRAY);

我正在尝试使用 Doctrine Query Builder 运行这个查询,但是 returns 这个错误:Doctrine\ORM\Query\QueryException [Semantical Error] ...: Error: Class 'NULL' is not defined. when using NULL 我做错了什么?如何在我的查询中实现 IS NOT NULL

编辑

此外,请考虑一下:

$result = $this->er->createQueryBuilder("r")
                ->select(
                         "COUNT(r.customer) as customers_total", 
                         "COUNT(r.customer IS NOT NULL) as customers_set", 
                         "COUNT(r.customer IS NULL) as customers_unset"
                )
                ->getQuery()
                ->getResult(AbstractQuery::HYDRATE_ARRAY);

因此,添加 ->where("r.customer IS NOT NULL") 在这里没有多大用处。我的第一个问题没有准确定义。我很抱歉。

试试这个

$result = $this->er->createQueryBuilder("r")
            ->select("COUNT(*) as customers")
            ->where("r.customer IS NOT NULL")
            ->getQuery()
            ->getResult(AbstractQuery::HYDRATE_ARRAY);

编辑:

如果您想要更复杂的请求

    $this->er->createQueryBuilder("r")
                    ->select("COUNT(r.customer) as customers_total,
COUNT(case when r.customer IS NOT NULL then 1 end) as customers_set,
COUNT(case when r.customer IS NULL then 1 end) as customers_unset
")
                    ->getQuery()
                    ->getResult(AbstractQuery::HYDRATE_ARRAY);

这个解决方案适合我:

$result = $er
            ->createQueryBuilder("r")
            ->select("COUNT(r.customer) as customers_total")
            ->addSelect("SUM(CASE WHEN r.customer IS NOT NULL THEN 1 ELSE 0 END) as customers_set")
            ->addSelect("SUM(CASE WHEN r.customer IS NULL THEN 1 ELSE 0 END) as customers_unset")
            ->getQuery()
            ->getResult(AbstractQuery::HYDRATE_ARRAY);