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
Symfony2 Doctrine throw NonUniqueResultException
提问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 getSingleResult
function
您可以检查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 LIMIT
to query and get only one result with ->setMaxResults(1)
.
解决问题您可以设置LIMIT
为查询并只获得一个结果->setMaxResults(1)
。
回答by AlterPHP
Don't use getSingleResult
if 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
getSingleResult
and deal with exception (like atry {...} catch (NonUniuqueResultException $e) {...}
or adjust your DB structure to avoid duplicates, - Use
getSingleResult
and addsetMaxResults(1)
, but this is really a strange way to trust your DB model, - Use
getResult
and 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。