Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No result was found for query although at least one row was expected

Tags:

I have the following code:

/**      * Search similar category given a string      *       * @param $searchTerm search similar category      */     public function findOneSimilarCategory($searchTerm)     {         $query = $this->createQueryBuilder('secondLevelCategory')                 ->select('secondLevelCategory')                 ->where('secondLevelCategory.categoryTitle LIKE :searchTerm')                 ->setParameter('searchTerm', $searchTerm)                 ->getQuery();          $query->useResultCache(true, self::CACHE_RESULT_LIFETIME, md5(__METHOD__ . serialize('category-search-' . $query->getParameters())));         $query->useQueryCache(true);         return $query->getSingleResult();     } 

however this gives me an error:

"PHP message: PHP Fatal error: Uncaught exception 'Doctrine\ORM\NoResultException' with message 'No result was found for query although at least one row was expected.' in /var/www/Shopious/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:649

like image 361
adit Avatar asked Mar 24 '15 08:03

adit


1 Answers

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.

like image 159
D_R Avatar answered Oct 20 '22 05:10

D_R