Doctrine2 ...最佳水合模式? [英] Doctrine2...Best hydration mode?

查看:108
本文介绍了Doctrine2 ...最佳水合模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个有九个实体的房间预订系统,这些都是相互关联的。在这个具体实例中,我从实体条目中检索10-30行,其中有25个属性。每个条目有一个房间,其中有10个属性。我需要所有的条目信息以及 entry-> room-> id entry-> room-> name 。但是当我使用 Query :: HYDRATE_ARRAY 时,似乎学说正在加载整个房间。似乎在 Query :: HYDRATE_OBJECT 中更容易加载。

I am designing a room booking system which has nine entities, which all relate to each other. In this specific instance I am retrieving 10-30 rows from the entity entry which has 25 properties. Each entry has one room which has 10 properties. I need all of the entry information as well as entry->room->id and entry->room->name. But it seems like doctrine is loading the entire room when I use Query::HYDRATE_ARRAY. It seems to be lazy-loading in Query::HYDRATE_OBJECT more easily.

所以,我想知道如果使用 Query :: HYDRATE_OBJECT 模式比更快或更好Query :: HYDRATE_ARRAY / Query :: HYDRATE_SCALAR / Query :: HYDRATE_SINGLE_SCALAR 。因为我正在重用一些旧的代码,我想使用 HYDRATE_ARRAY ,只有当它不会减慢应用程序的速度。

So, I am wondering if using the Query::HYDRATE_OBJECT mode is faster or "better" than Query::HYDRATE_ARRAY / Query::HYDRATE_SCALAR/ Query::HYDRATE_SINGLE_SCALAR. Since I am reusing some older code I'd like to use HYDRATE_ARRAY but only if it won't slow the application down.

推荐答案

我的2美分:

HYDRATE_OBJECT 当您计划在对象上使用大量业务逻辑时。特别是如果你做了大量的数据操作。这也许是最慢的(取决于情况)。

HYDRATE_OBJECT is best for when you plan on using a lot of business logic with your objects. Especially if you're doing a lot of data manipulation. It's also probably the slowest (depending on the situation).

HYDRATE_ARRAY 通常保留用于当您只需要一个结果和1度的关系数据,它将仅用于打印/查看目的。

HYDRATE_ARRAY is usually reserved for when you only need a result and 1 degrees of relational data and it's going to be used for printing/viewing purposes only.

HYDRATE_NONE 是另一个我使用的,当我只选择一个非常小的数据子集(如一个或两个字段而不是整个行)。这个行为很像一个原始的查询结果。

HYDRATE_NONE is another one I use when I'm only selecting a very small subset of data (like one or two fields instead of the entire row). This behaves much like a raw query result would.

这也可能是感兴趣的 http://www.doctrine-project.org/2010/03/17/doctrine-performance-revisited.html

这是从1.2个文档,但我认为水化提示适用于2.0 http://doctrine.readthedocs.org/zh/latest/en/manual/improving-performance.html

This is from the 1.2 docs but I think the Hydration tips apply in 2.0 http://doctrine.readthedocs.org/en/latest/en/manual/improving-performance.html


属于此类别的另一个重要规则是:只有在您真正需要时才能提取对象。教义有能力取代数组图而不是对象图。乍看起来,这可能听起来很奇怪,因为为什么首先使用对象关系映射器?花一点时间考虑一下。 PHP本质上是一个先进的语言,已经增强了许多功能,用于体面的OOP。数组仍然是您可以在PHP中使用的最有效的数据结构。对象在用于完成复杂的业务逻辑时具有最大的价值。当您没有使用

Another important rule that belongs in this category is: Only fetch objects when you really need them. Doctrine has the ability to fetch "array graphs" instead of object graphs. At first glance this may sound strange because why use an object-relational mapper in the first place then? Take a second to think about it. PHP is by nature a precedural language that has been enhanced with a lot of features for decent OOP. Arrays are still the most efficient data structures you can use in PHP. Objects have the most value when they're used to accomplish complex business logic. It's a waste of resources when data gets wrapped in costly object structures when you have no benefit of that

使用 HYDRATE_ARRAY :


你能想到在视图中使用对象而不是数组的任何好处吗?你不会执行业务逻辑,是吗?一个参数可以节省大量不必要的处理:

Can you think of any benefit of having objects in the view instead of arrays? You're not going to execute business logic in the view, are you? One parameter can save you a lot of unnecessary processing:



$blogPosts = $q->execute(array(1), Doctrine_Core::HYDRATE_ARRAY);

这篇关于Doctrine2 ...最佳水合模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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