php Symfony2 学说抛出 NonUniqueResultException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17798252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:28:28  来源:igfitidea点击:

Symfony2 Doctrine throw NonUniqueResultException

phpsymfonydoctrine-orm

提问by Ajouve

I have a problem throwing NonUniqueResultException in my request

我在请求中抛出 NonUniqueResultException 时遇到问题

public function getLastViewUpdate($view)
{
    $qb = $this->getEntityManager()->createQueryBuilder();

    $result = $qb->select('vu')
        ->from('EasyApp\ApplicationBundle\Entity\ViewUpdate', 'vu')
        ->where('vu.view = :view')
        ->orderBy('vu.date','DESC')
        ->setParameter('view', $view)
        ->getQuery()
        ->getSingleResult();

    return $result;
}

But I don't know realy why, I have maybe to import something, but I can't find

但我真的不知道为什么,我可能要导入一些东西,但我找不到

CRITICAL - Uncaught PHP Exception Doctrine\ORM\NonUniqueResultException: "" at /Users/antoine/Documents/projects/easyApp/application/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php line 621 

Thanks for your help

谢谢你的帮助

回答by Alexey B.

You can check declaration of getSingleResultfunction

您可以检查getSingleResult函数的声明

/**
 * Gets the single result of the query.
 *
 * Enforces the presence as well as the uniqueness of the result.
 *
 * If the result is not unique, a NonUniqueResultException is thrown.
 * If there is no result, a NoResultException is thrown.
 *
 * @param integer $hydrationMode
 * @return mixed
 * @throws NonUniqueResultException If the query result is not unique.
 * @throws NoResultException If the query returned no result.
 */
public function getSingleResult($hydrationMode = null)
{
    ...
    if (count($result) > 1) {
        throw new NonUniqueResultException;
    }
    ...
}

To solve the problem You can set LIMITto query and get only one result with ->setMaxResults(1).

解决问题您可以设置LIMIT为查询并只获得一个结果->setMaxResults(1)

回答by AlterPHP

Don't use getSingleResultif you expect more than 1 result... Using this function performs a unicity check of your result, it's the intention of this function.

getSingleResult如果您期望超过 1 个结果,请不要使用...使用此函数对您的结果执行唯一性检查,这是此函数的意图。

Many choices :

许多选择:

  • Use getSingleResultand deal with exception (like a try {...} catch (NonUniuqueResultException $e) {...}or adjust your DB structure to avoid duplicates,
  • Use getSingleResultand add setMaxResults(1), but this is really a strange way to trust your DB model,
  • Use getResultand do something with returned results.
  • 使用getSingleResult和处理异常(例如try {...} catch (NonUniuqueResultException $e) {...}或调整您的数据库结构以避免重复,
  • 使用getSingleResult和添加setMaxResults(1),但这确实是一种信任您的数据库模型的奇怪方式,
  • 使用getResult并使用返回的结果做一些事情。

回答by Cerad

It just means that you have two or more ViewUpdates with the same view.

这只是意味着您有两个或多个具有相同视图的 ViewUpdate。