How to load a one

While I'll go for the Ghost pattern in a 1:1 relationship, I'm not sure if this is sufficient in a 1:n relationship.

For example, when I load an Order object that may have a hundred Item objects, I would first assign NULL to the items property.

The question is:

A) Should I assign NULL and then, upon first access of the items property look for ALL orders of that item and load ALL of them?

B) Or should I load only a lightweight record set containing only all the order_id's and load 100 Item Ghost objects which only have the order_id set, but everything else NULL - and as soon as an Item object is accessed to fetch more details from it, lazy-load that specific Item object data?


Well IMO your needs for lazy-loading differ in every situation. However, I would say that if you have an orders large enough to have 100 items on them there is rarely a time when you want to load all 100 items at the same time.

What I would recommend is that you load each item on demand, allowing you to load a "page" of items or just one item. For example, if you needed to list all of the items you could load "page" that list and since each item can be loaded on demand you would just load the selected page. Further, for performance, you probably want to recycle those objects so you might consider building say 10 Ghost items and then load a "page" at a time into those 10 items.

On the other hand, since an item is loaded on demand as such, you could just load one item allowing you to show the full details of that item, only when you wanted.

链接地址: http://www.djcxy.com/p/56222.html

上一篇: PHP MVC:数据映射器模式:类设计

下一篇: 如何加载一个