原则返回null代替EntityNotFoundException [英] Doctrine return null in place of EntityNotFoundException

查看:40
本文介绍了原则返回null代替EntityNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中的FK损坏,如果我加载一个实体并要求相关的实体,Doctrine将抛出 \ Doctrine \ ORM \ EntityNotFoundException .

I have broken FKs in my database and if I load an entity and ask for a related entity Doctrine will throw \Doctrine\ORM\EntityNotFoundException.

对于有问题的实体,我希望FK损坏的地方将返回NULL而不是引发异常.这是因为在Twig模板中会发生异常,因此我希望Twig在这种情况下不必处理异常.

For the entity in question, I would prefer that where the FK is broken it would return NULL rather than throw an exception. This is because its within a Twig template that the exception occurs and I would prefer Twig to not have to have to handle the exception in this case.

以下是示例配置.

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="Foo\Click" table="clicks">
        <id name="id" type="bigint" column="click_id">
            <generator strategy="IDENTITY"/>
        </id>
        <!-- .. -->
        <many-to-one field="visitor" target-entity="Foo\Visitor" fetch="LAZY">
            <join-columns>
                <join-column name="visitor_id" referenced-column-name="visitor_id"/>
            </join-columns>
        </many-to-one>
    </entity>

    <entity name="Foo\Visitor" table="visitors" read-only="true">
        <id name="visitorId" type="integer" column="visitor_id">
            <generator strategy="IDENTITY"/>
        </id>
        <!-- ... -->
        <one-to-one field="firstClick" target-entity="Foo\Click" fetch="LAZY">
            <join-columns>
                <join-column name="click_id" referenced-column-name="click_id"/>
            </join-columns>
        </one-to-one>
    </entity>
</doctrine-mapping>

以下是预期结果的示例,其中单击作为访问者ID,但该ID不存在访问者记录.在这种情况下,我宁愿不必将逻辑包装在Try/Catch中,而是让 Click :: getVisitor()返回 null ;

The following is an example of expected results where the the click as a visitor ID, but the a visitor record does not exists with that ID. In this case, I would rather not have to wrap the logic in Try/Catch and instead have Click::getVisitor() return null;

<?php
$clickOne = $entityManager()->find(Foo\Click::class, 1);
$v = $clickOne->getVisitor();

if ($v !== null) {
    echo $v->getId(); // may throw Doctrine\ORM\EntityNotFoundException
}

学说有针对性的策略吗?

Is there a strategy for this with Doctrine?

更新:添加了示例配置和代码,现在我知道为什么使用简单的Doctrine配置无法实现这一点.

Update: Added example configuration and code, and now I see the why this is not achievable with a simple Doctrine configuration.

推荐答案

这是我根据 iainn 发表的in-place-of-entitynotfoundexception#comment90737798_51893358>评论.

This is the strategy I have adopted based on the comment made by iainn.

<?php
class Parent
{
    protected $child;

    public function getChild()
    {
        if ($this->child instance of \Doctrine\ORM\Proxy\Proxy) {
            try {
                $this->child->__load();
            } catch (\Doctrine\ORM\EntityNotFoundException $e) {
                $this->child = null
            }
        }

        return $this->child;
    }
}

我确定不建议您的实体直接与代理互动.但是我宁愿选择这种方法,而不是在实体上调用已知方法(这也将具有加载方法的作用),因为对于下一个可能会读它的开发人员来说,此代码的意图更加清楚.

I am sure it is not recommended for your entities to interact directly with proxies. But I preferred this over calling a known method on the entity (which would also have the effect of loading it) because the intention of this code is clearer to the next developer who might read it.

我不确定与这样的代理进行交互是否有任何副作用.

I'm not sure if there are any side effects with interacting with the proxy like this.

这篇关于原则返回null代替EntityNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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