【问题标题】:Symfony + Doctrine - Define an error message when integrity constraint errorSymfony + Doctrine - 完整性约束错误时定义错误消息
【发布时间】:2016-05-30 21:32:47
【问题描述】:

当我尝试删除项目时出现完整性约束错误时,我正在尝试显示一个很好的错误消息。

我只想显示一条消息,而不是出现错误 500: "您不能删除它,因为有些项目已链接到它"

我一直在寻找一段时间,但我总是找到关于“如何解决这个错误”的解决方案。我不会不解决它,我只是想捕捉错误,就像带有消息参数的@UniqueEntity 注释一样。

【问题讨论】:

    标签: php symfony error-handling doctrine-orm


    【解决方案1】:

    您可以实现一个EventListener 来监听PDOException

    // src/CM/PlatformBundlee/EventListener/PDOExceptionResponseListener.php
    
    namespace CM\PlatformBundle\EventListener;
    
    use Symfony\Component\HttpFoundation\Response;
    use Doctrine\DBAL\Driver\PDOException;
    use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
    use Symfony\Component\HttpFoundation\Session\SessionInterface;
    
    class PDOExceptionResponseListener
    {
        public function __construct(SessionInterface $session) 
        {
            $this->session = $session;
        }
    
        /**
         * @param GetResponseForExceptionEvent $event
         */
        public function onKernelResponse(GetResponseForExceptionEvent $event)
        {
            $request = $event->getRequest();
    
            $exception =  $event->getException();
            $message = $exception->getMessage();
    
            // Listen only on the expected exception
            if (!$exception instanceof PDOException) {
                return;
            }
    
            // You can make some checks on the message to return a different response depending on the MySQL error given.
            if (strpos($message, 'Integrity constraint violation')) {
                // Add your user-friendly error message
                $this->session->getFlashBag()->add('error', 'PDO Exception :'.$message);   
            }
        }
    }
    

    将其声明为服务:

    // app/config/services.yml
    services:
        acme.kernel.listener.pdo_exception_response_listener:
            class: CM\PlatformBundle\EventListener\PDOExceptionResponseListener
            tags:
                - {name: kernel.event_listener, event: kernel.exception, method: onKernelResponse}
            arguments: 
                session: "@session"
    

    让您的模板显示会话消息:

    // twig
    {% for flashMessage in app.session.flashbag.get('error') %}
        {{ flashMessage }} 
    {% endfor %}
    

    编辑

    如果您想在特定操作上拦截此错误,可以这样做:

    try {
        $em->flush();
    } catch (\Exception $e) {
        $errorMessage = $e->getMessage();
        // Add your message in the session
        $this->get(‘session’)->getFlashBag()->add('error', 'PDO Exception :'.$errorMessage);   
    }
    

    【讨论】:

    • 谢谢!我尝试添加此内容,但出现Attempted to load class "PDOExceptionResponseListener" from namespace "CM\PlatformBundle\EventListener". 错误。我可能会在某个地方错过一些东西。 :s
    • 我已经编辑了答案中的代码。现在它使用您的命名空间,并查看有关文件位置的注释,将其命名为与类名完全相同。但是,如果您只需要对一项操作执行此操作,请使用我编辑中的简短解决方案。
    • 感谢您的帮助。我已经改变了一切,感谢您的编辑。但我对这条线有疑问:acme.kernel.listener.exception_listener: 我应该保持这样吗?
    • 这一行是您的服务的标识符。您可以根据自己的喜好更改它。可以是 cm.platform.pdo_exception_response_listener 。你肯定永远不会使用它。
    • 好吧,我修好了!我刚刚调用了我的文件“ExceptionResponseListener.php”,我没有注意到你改变了它。现在它得到了错误但是: - 它没有捕捉到错误(这条线不起作用:if (!$exception instanceof PDOException) { return; })我不知道为什么,但我会找到的。 -我也有这个错误Attempted to call an undefined method named "get" of class "CM\PlatformBundle\EventListener\PDOExceptionResponseListener".我会尝试自己修复它。谢谢!
    猜你喜欢
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    相关资源
    最近更新 更多