mysql - "No result was found for query although at least one row was expected."查询应该在 Symfony 中显示记录

标签 mysql doctrine doctrine-orm symfony

我正在尝试使用 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 the getOneOrNullResult() 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/

相关文章:

php - 如何限制学说 2 中结果集的大小?

mysql - 当我在 sql 中使用 Not Like 时无法识别的关键字接近

php - 获取特定行的总和值

doctrine - Doctrine 中的存储库是什么?

php - 在 Symfony/Doctrine 中删除大量用户数据的最佳/快速解决方案

mongodb - 在 symfony 2.1 中使用 mongodb 进行用户身份验证

mysql - rails 中的多个数据库连接

php - 使用 PHP 将分隔值文件导入数据库时​​如何删除或避免插入列标题?

mysql - 是否可以在不使用关系的情况下连接 doctrine ORM 中的表?

mongodb - Doctrine MongoDB ODM 的原子操作