Symfony2 Doctrine throw NonUniqueResultException

doctrine-orm, php, symfony

Solution

You can check declaration of `getSingleResult` function

/**
 * 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)`.

Problem

I have a problem throwing NonUniqueResultException in my request ``` 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

Original source