rkmax
rkmax

Reputation: 18125

Why Symfony2 dont catch exceptions

i have a repository

class TurnoRepository extends EntityRepository
{
    public function findTurnoActivo()
    {
        $q = $this
            ->createQueryBuilder('t')
            ->where('t.activo = :activo')
            ->setParameter('activo', true)
            ->getQuery();

        return $q->getSingleResult();
    }
}

this method throw a NoResultException but if i try to catch in my controller

private function obtenerTurno()
{
    $em = $this->getDoctrine()->getEntityManager();
    $turno = null;

    try {
        $turnoActivo = $em->getRepository('MyBundle:Turno')->findTurnoActivo();
    } catch (NoResultException $e) {
        return false;
    }

    return $turno;

}

always i get 500 Internal Server Error on my page

Upvotes: 3

Views: 4011

Answers (2)

Mun Mun Das
Mun Mun Das

Reputation: 15002

You can use $q->getOneOrNullResult(); if you don't want to catch NoResultException in the controller.

Upvotes: 4

Besnik
Besnik

Reputation: 6529

Symfony2 code is namespaced so you have to add the correct namespace for the class NoResultException, try using:

catch (\Doctrine\ORM\NoResultException $e)

Note the backslash in front of the Doctrine namespace or import the NoResultException class by using use.

Upvotes: 10

Related Questions