为什么Symfony2不会遇到异常 [英] Why Symfony2 dont catch exceptions

查看:83
本文介绍了为什么Symfony2不会遇到异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储库

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

        return $q->getSingleResult();
    }
}

此方法抛出NoResultException,但如果我尝试捕获在我的控制器中

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;

}

总是我得到500内部我的页面上的服务器错误

always i get 500 Internal Server Error on my page

推荐答案

Symfony2代码命名空间,因此您必须为类添加正确的命名空间 NoResultException ,尝试使用:

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

catch (\Doctrine\ORM\NoResultException $e)

请注意Doctrine命名空间前面的反斜杠或导入 NoResultException class通过使用使用

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

这篇关于为什么Symfony2不会遇到异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆