Symfony:在教义关系上处理EntityNotFoundException [英] Symfony: Handling EntityNotFoundException on Doctrine relation

查看:48
本文介绍了Symfony:在教义关系上处理EntityNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在Twig中显示相关对象时,由于ID在父实体中,而该相关实体不在当前数据库中,因此不存在该关系,Symfony抛出500错误

When I tried to display a related object in Twig, and that relation is not present because the ID is in the parent entity, but the related entity was not in the current database, Symfony throws a 500 error

//EntityNotFoundException Twig_Error_Runtime呈现模板期间引发了异常("ID为(265)的类型为'App \ Entity \ Location'的类型的实体不是找到").

// EntityNotFoundException Twig_Error_Runtime An exception has been thrown during the rendering of a template ("Entity of type 'App\Entity\Location' for IDs id(265) was not found").

我希望能够忽略此错误,而显示诸如相关对象丢失"之类的内容.

I'd like to be able to ignore this error and instead display something like "Related object missing".

这似乎可以通过在Twig中进行一些检查来解决,但是检查该关系是否为null或未定义是行不通的-它们都仍然可以找到该关系,但是当在相关实体上调用属性时,该异常被抛出.

It seemed like this could be solved by some checking in Twig, but checking if the relation is null or not defined does not work - they both still find the relation, but when a property is called on the related entity, the exception is thrown.

有人解决了这个问题吗?

Has anyone solved this problem already?

推荐答案

您可以检查Twig扩展名中是否存在该实体

You could check if the entity exists in a Twig extension

类似:

public function isRelatedEntityDefined($entity)
{    
   try {
        if(isset($entity->getSomeField()) return true;        
   } catch (EntityNotFoundException $e) {
        return false;
   }
}

这篇关于Symfony:在教义关系上处理EntityNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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