首页 > 解决方案 > 教义查询构建器,可空字段的 where 语句

问题描述

当 2 个字段可以为空时,我需要创建查询。

询问:

 $qb = $this->entityManager->createQueryBuilder();
 $qb->select('t')
    ->from('Table', 't')
    ->where('t.name = :name')
    ->andWhere('t = :country')
    ->andWhere('t = :language')
    ->setParameters([
        'name' => $info->getName(),
        'country' => $info->getCountry(),
        'language' => $info->getLanguage()
     ]);

$result = $qb->getQuery()->getOneOrNullResult();

在所有字段不为空的情况下一切正常,但在我的情况下,country可以language为空,并且学说将其转换为查询,例如where country = nullwhich is not working 并且我没有得到任何结果,因为它应该是where country is null.

所以我的问题是有一种方法可以处理它并自动转换is null为提供的参数为空时,还是我需要使用 phpif语句自行检查?如果我应该自己检查它,这很烦人且容易出错,因为您需要始终知道哪些字段可以为空,并对每个可以为空的字段进行此检查。

仅供参考:我正在使用 Postgres

标签: postgresqldoctrine-orm

解决方案


推荐阅读