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

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

问题描述

情况:



我想使用函数的ORM / EntityManager.php#L374-413。但是,在我要求从数据库中删除的对象的情况下,如果我多次请求相同的对象,我将获得代理。



例如:



  //已从数据库中删除的随机文章对象
$ articleClass =' Acme\ArticleBundle\Entity\Article;
$ articleIdentifiers = array('id'=> 1);
$ i = 0;

//我们要求它的引用两次
do {
try {
echoa;
$ subject = $ this-> em-> getReference(
$ subjectClass,
$ subjectIdentifiers
);
//现在调用此对象
var_dump($ subject);

} catch(\Exception $ e){
echob;
}
$ i ++;
} while($ i <2);



输出结果:



  a 
b
a
对象(Proxies\__CG__\Acme\ArticleBundle\Entity\Article)
/ pre>

如何获取数据库中甚至不存在的对象的代理?如果我发表评论这一行,entityManager不管理对象,我获得输出 abab ,这对我来说更有意义,因为我不想得到数据库中不存在的代理对象。对于info,返回的代理对象具有所有属性 null 。因此,我获取数据库中不存在的对象的代理。所以,如果我要求这个对象,我得到一个未找到的实体例外。



挑战



任何人都可以感觉到这一点吗?有没有办法依靠 getReference()告诉我们这个对象是否真的存在于数据库中?

解决方案

没有办法使 getReference()检查数据库是否存在引用对象。



实际上,这就是 getReference(),并且它返回的代理都是关于:创建占位符对象(代理)而不去数据库。而且你很少想明确地这样做。通常,Doctrine在内部实现这一点,当实体根据外键值为相关实体创建延迟加载占位符时,实现这一点。



为什么不调用 find()在实体管理器上?您是否知道,只要您通过ID查找,EM将不会查询同一个对象的数据库?原则在工作单位中跟踪已经水化的对象,并在随后的 find()调用中返回对现有对象的引用。


Situation:

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.

An example:

//A random article object...that has been deleted from the database
$articleClass = 'Acme\ArticleBundle\Entity\Article';
$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);

The output:

a
b
a
object(Proxies\__CG__\Acme\ArticleBundle\Entity\Article)

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.

The challenge

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?

解决方案

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

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.

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()的doctrine实体管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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