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

查看:88
本文介绍了什么是原则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. 什么是代理类?

  2. 我应该何时使用它们实体?

  1. What are Proxy classes?
  2. When should I use them over entities?

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

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?

推荐答案

当您的查询未返回创建实体所需的所有数据时,将使用代理对象。想象下面的情况:

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 Entity\User u WHERE u.id = :id

正如您可以看到此查询不返回 firstname lastname 属性,因此您无法创建用户对象。创建不完整的实体可能会导致意外的错误。

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.


为什么延迟加载不能在Entitiy本身中实现? >

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.


你可以提供一个用例吗?

Can you provide me an use case?

您正在显示最新的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 Entity\Article 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;
    }
}






更新



在下面的注释部分中,有关代理对象和部分对象之间差异的错误信息。有关详细信息,请参阅@Kontrollfreak答案: http://stackoverflow.com/a/17787070/252591

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

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