我正在尝试使用 URL 中的两个项目来检索内容。这是应该执行此操作的 php/symfony 代码:
$em = $this->getDoctrine()->getEntityManager();
$repository = $this->getDoctrine()
->getRepository('ShoutMainBundle:Content');
$query = $repository->createQueryBuilder('p')
->where('p.slug > :slug')
->andWhere('p.subtocontentid > :parent')
->setParameters(array(
'slug' => $slug,
'parent' => $page
))
->getQuery();
$content = $query->getSingleResult();
但是,当执行此代码时,它会返回以下错误:
No result was found for query although at least one row was expected.
我做了一些测试,$slug
和 $page
变量中保存的数据保存了正确的信息。我还测试了 MySQL 查询,查询显示了所需的结果,这让我更加困惑。
我错过了什么吗?
最佳答案
正如回答here
You are getting this error because you are using the
getSingleResult()
method. it generates an Exception if it can't find even a single result. you can use thegetOneOrNullResult()
instead to get a NULL if there isn't any result from the query.Query#getSingleResult(): Retrieves a single object. If the result contains more than one object, an NonUniqueResultException is thrown. If the result contains no objects, an NoResultException is thrown. The pure/mixed distinction does not apply.
关于mysql - "No result was found for query although at least one row was expected."查询应该在 Symfony 中显示记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7122982/