学说实体管理器的getReference() [英] getReference() of doctrine Entity Manager

查看:17
本文介绍了学说实体管理器的getReference()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 getReference() Dotct2 实体管理器的功能.但是,在我请求已从数据库中删除的对象的情况下,如果我多次请求同一个对象,我将获得一个代理.

I want to use the getReference() function of doctrine2 Entity Manager. However, in a situation where I ask for an object that has been deleted from the database, I obtain a proxy if I ask for that same object more than once.

//A random article object...that has been deleted from the database
$articleClass = 'AcmeArticleBundleEntityArticle';
$articleIdentifiers = array('id'=>1);
$i = 0;

//We ask for its reference twice
do{
    try {
        echo "a";
        $subject = $this->em->getReference(
            $subjectClass,
            $subjectIdentifiers
        );
       //call this object now
       var_dump($subject);

    } catch (Exception $e) {
        echo "b";
    }
    $i++;
} while ($i <2);

输出:

a
b
a
object(Proxies\__CG__AcmeArticleBundleEntityArticle)

如何为数据库中甚至不存在的对象获取代理?如果我评论这一行,则entityManager不管理对象,我获得了输出 abab,这对我来说更有意义,因为我不想获得数据库中不存在的代理对象.有关信息,返回的代理对象具有其所有属性 null.因此,我获得了数据库中不存在的对象的代理.所以,如果我要求这个对象,我会得到一个未找到实体"的异常.

How can I get a proxy for an object that doesn't even exist in the database? If I comment this line, the entityManager does not manage the object and I obtain the output abab, which to me makes more sense as I don't want to get a proxy object that does not exist in the database. For info, the proxy object returned has all its properties null. I therefore obtain a proxy for an object that does not exist in the database. So, if I ask for this object I get a "Not found Entity" exception.

有人能理解吗?有没有办法依靠getReference()来告诉我们这个对象是否真的存在于数据库中?

Can anyone make any sense of this? Is there a way to rely on getReference() to tell us whether this object really does exist in the database?

推荐答案

没有办法让 getReference() 检查数据库中是否存在被引用的对象.

There is no way to make getReference() check the database for the existence of the referenced object.

实际上,这就是 getReference() 及其返回的代理的全部内容:无需访问数据库即可创建占位符对象(代理).你很少会想明确地这样做.通常,Doctrine 在内部执行此操作时,会根据外键值为相关实体创建延迟加载占位符.

Actually, this is what getReference() and the proxies returned by it are all about: Creating placeholder objects (proxies) without going to the database. And you would rarely want to do that explicitly. Normally Doctrine does this internally when hydrating entities to create lazy loading placeholders for related entities based on foreign key values.

为什么不直接在实体管理器上调用 find() ?您是否知道只要您通过 ID 查找同一个对象,EM 就不会多次查询数据库?Doctrine 会跟踪工作单元中已经水合的对象,并在后续的 find() 调用中返回对现有对象的引用.

Why don't you just call find() on the Entity Manager? Are you aware that the EM will not query the DB more than once for the same object as long as you look it up by ID? Doctrine keeps track of already hydrated objects in the Unit Of Work and returns references to the existing objects in subsequent find() calls.

这篇关于学说实体管理器的getReference()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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