Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No result was found for query although at least one row was expected." Query should display records though in Symfony

I'm trying to retrieve content using two items in the URL. Here is the php/symfony code that should do it:

    $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();

However, when this code is executed it returns the following error:

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

I have done some tests, and the data held in the $slug and $page variables hold the correct information. I have also tested the MySQL query and the query brings up the desired result, which confuses me further.

Have I missed something?

like image 588
mickburkejnr Avatar asked Aug 19 '11 14:08

mickburkejnr


2 Answers

As it was answered 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.

like image 197
pyjavo Avatar answered Nov 04 '22 09:11

pyjavo


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

Another reason could be:

You did this

$query = $this->getEntityManager()
            ->createQuery('
                SELECT u FROM MyBundle:User u
                WHERE u.email = :email')
            ->setParameter('email', $email);

return $query->getSingleResult();

Instead of this

$query = $this->getEntityManager()
            ->createQuery('
                SELECT u FROM MyBundle:User u
                WHERE u.email = :email')
            ->setParameter('email', $email);

$query->setMaxResults(1);

return $query->getResult();
like image 4
Jonathan Avatar answered Nov 04 '22 11:11

Jonathan