什么是 Doctrine 2 中的代理? [英] What is a Proxy in Doctrine 2?

查看:16
本文介绍了什么是 Doctrine 2 中的代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚读完了所有的Doctrine 2文档,我开始了我自己的沙箱,我理解了大部分原理,但仍然有一个问题,我在文档中找不到任何完整的解释.

I just finished reading all the Doctrine 2 documentation, I started my own sandbox, I understood most of the principes, but there is still a question and I couldn't find any complete explanation in the doc.

  1. 什么是Proxy 类?
  2. 我什么时候应该在实体上使用它们?

据我所知,代理类添加了一个层,让您向实体添加一些其他功能,但为什么要使用代理而不是在实体类中实现方法本身?

As far as I understand, proxy classes add a layer to let you add some other features to your entities, but why use a proxy instead of implementing the methods themselves in the entity class?

推荐答案

更新

此答案包含有关代理对象和部分对象之间差异的错误信息.有关更多详细信息,请参阅 @Kontrollfreak 的回答:https://stackoverflow.com/a/17787070/252591

<小时>

只要您的查询未返回创建实体所需的所有数据,就会使用代理对象.想象以下场景:


Proxy objects are used whenever your query doesn't return all data required to create an entity. Imagine following scenario:

@Entity
class User {
     @Column protected $id;
     @Column protected $username;
     @Column protected $firstname;
     @Column protected $lastname;

     // bunch of setters/getters here
}

DQL query:

SELECT u.id, u.username FROM EntityUser u WHERE u.id = :id

如您所见,此查询不返回 firstnamelastname 属性,因此您无法创建 User 对象.创建不完整的实体可能会导致意外错误.

As you can see this query doesn't return firstname and lastname properties, therefore you cannot create User object. Creation of incomplete entity could lead to unexpected errors.

这就是为什么 Doctrine 将创建支持延迟加载的 UserProxy 对象.当您尝试访问 firstname 属性(未加载)时,它将首先从数据库加载该值.

That's why Doctrine will create UserProxy object that supports lazy loading. When you'll try to access firstname property (which is not loaded) it will first load that value from database.

我的意思是我为什么要使用代理?

I mean why should I use a proxy ?

您应该始终像根本不使用代理对象一样编写代码.它们可以被视为 Doctrine 使用的内部对象.

You should always write your code as if you didn't use proxy objects at all. They can be treated as internal objects used by Doctrine.

为什么懒加载不能在实体本身实现?

Why the lazy loading can't be implemented in the Entitiy itself?

从技术上讲,它可能只是看看一些随机代理对象的类.它充满了肮脏的代码,呃.在您的实体中拥有干净的代码真是太好了.

Technically it could be but take a look at some random proxy object's class. It's full of dirty code, ugh. It's nice to have a clean code in your entities.

你能给我一个用例吗?

您正在显示最新 25 篇文章的列表,并且想要显示第一篇文章的详细信息.它们中的每一个都包含大量文本,因此获取所有数据将浪费内存.这就是您不获取不必要数据的原因.

You're displaying a list of latest 25 articles and you want to display a details of the first one. Each of them contain a large amount of text, so fetching all that data would be a waste of memory. That's why you don't fetch unnecessary data.

SELECT a.title, a.createdAt
FROM EntityArticle a
ORDER BY a.createdAt DESC
LIMIT 25

$isFirst = true;
foreach ($articles as $article) {
    echo $article->getTitle();
    echo $article->getCreatedAt();

    if ($isFirst) {
        echo $article->getContent(); // Article::content is not loaded so it is transparently loaded 
                                     // for this single article.

        $isFirst = false;
    }
}

这篇关于什么是 Doctrine 2 中的代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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