doctrine查询生成器失败

时间:2016-10-01 13:53:06

标签: php symfony doctrine-orm doctrine

我有以下doctrine查询builer,它返回了我的错误:

No result was found for query although at least one row was expected. 

我怀疑错误的发生是因为它无法找到其中一个关联,因为并非所有字段都具有该关联。 我相信的问题是:

->join('b.answer', 'c')

方法:

  public function getOneByStep($step, $surveyId)
    {
        $qb = $this->getEntityManager()->createQueryBuilder();

        return $qb
            ->select(['u', 'b', 'c'])
            ->from(QuestionManager::class, 'u')
            ->join('u.survey', 'a')
            ->join('u.suggestQuestionManager', 'b')
            ->join('b.answer', 'c')
            ->where('u.step = :step')
            ->andWhere('a.id = :survey')
            ->setParameter('step', $step)
            ->setParameter('survey', $surveyId)
            ->getQuery()
            ->getSingleResult()
            ;
    }

1 个答案:

答案 0 :(得分:1)

给定查询不会返回数据库中数据的任何结果。理想情况下,应使用NoResultException处理。

use Doctrine\ORM\NoResultException;

public function getOneByStep($step, $surveyId)
{
    $qb = $this->getEntityManager()->createQueryBuilder();
        ->select(['u', 'b', 'c'])
        ->from(QuestionManager::class, 'u')
        ->join('u.survey', 'a')
        ->join('u.suggestQuestionManager', 'b')
        ->join('b.answer', 'c')
        ->where('u.step = :step')
        ->andWhere('a.id = :survey')
        ->setParameter('step', $step)
        ->setParameter('survey', $surveyId);

    try {
        return $qb->getQuery()->getSingleScalarResult();
    } catch (NoResultException $ex) {
        return;
    }
}