需要帮助来了解Magento产品集合对象和目录/产品模型

问题描述:

我继承了一个Magento项目,该项目由一位老开发人员负责. Magento经验非常有限,我想了解产品系列和catalog/product模型的工作方式.

I've inherited a Magento project which an old developer was in charge of. Having very limited Magento experience, I'd like to understand how product collections and the catalog/product model is working.

我特别想知道为什么集合和产品对象如此之大?

Specifically I'm wondering why the collection and product objects are so huge?

我有这样的代码:

<?php $_productCollection = $this->getLoadedProductCollection(); ?>
<?php foreach ($_productCollection as $_product): ?>
    <?php $_product = Mage::getModel( 'catalog/product' )->load( $_product->getId() ); ?>
    <?php print $this->getLayout()->createBlock('mymodule_name/category_products_item')->setProduct($_product)->toHtml() ?>
<?php endforeach ?>

当我执行print_r($_productCollection)print_r($_product)时,会得到大量数据,而我真正想要的只是一些产品属性(ID,名称,价格,图像URL等).

When I do print_r($_productCollection) or print_r($_product) I get massive amounts of data, when all I really want is a few product attributes (ID, name, price, image URL, etc.).

我习惯了一个自定义的关系数据库平台,如果需要使用产品,我会做一些像SELECT id,name,price FROM products;这样的超简单操作!我完全不确定如何在Magento中实现这一目标.我要创建自定义模型还是其他?

I'm used to a custom relational database platform where, if I needed to use products, I'd just do something ultra-simple like SELECT id,name,price FROM products;! I'm not at all sure how I'd achieve this in Magento. Do I create a custom model or something?

所以任何人都可以告诉我:

So can anyone please tell me:

1)为什么上述物体这么大?

1) Why are the above objects so huge?

2)如果我只需要一些产品属性(如上所述),那么有没有办法将对象限制为仅那些属性以减小其大小?

2) If I only need a few product attributes (as above) then is there a way to limit the objects to only those attributes to reduce their size?

3)以这种方式使用对象是否会对性能或内存造成影响?

3) Is there a performance or memory hit from using objects this way?

非常感谢!

您可以通过多种方式从magento访问数据:

You have several ways to access data from magento :

  • 使用load()=>加载相对于模型的所有数据(属性).这是非常缓慢的性能杀手.对于产品,您应该仅在产品页面上使用load()(因为该页面上使用的所有数据都是相对于单个对象的,所以您可以完整地加载它)

  • using load() => it loads ALL the data (attributes) relative to a model. It's very slow and performance-killer. For a product, you should use a load() only on the product page (because all the data you'll use on that page is relative to a single object .. so you can load it the full way)

使用collection =>当您需要检索对象列表时,必须(至少)使用一个collection.由您决定要检索的属性列表.您可以添加属性以选择/过滤,集合将在后台设法对EAV表等进行SQL连接

using collection => when you need to retrieve a list of objects, you must use (at least) a collection. It's up to you to decide of the attribute list you want to retrieve. You can add attribute to select/filter and the collection will manage to do the SQL joins to EAV tables etc... in background

使用自定义SQL =>集合在处理复杂对象时可能会很慢(一个集合会初始化许多SQL联接到您可能不需要的表)...从BDD访问数据的最后一种方法是创建自己的ResourceModel中的SQL

using custom SQL => collection can be slow when dealing with complex objects (a collection initialize many SQL joins to table you may not need)... the last method to access data from BDD is to create your own SQL in your ResourceModel

在显示的脚本中,有一个巨大的错误:您在对集合进行迭代的foreach中加载完整的模型.您绝对不能这样做,如果您必须load()产品,我想是因为您没有在集合中找到属性?在这种情况下,您只需要修改集合即可检索属性...

In the script you show, there is a huge error : you load a full model inside a foreach iterating on a collection. You must never do that, if you have to load() the product, I assume it's because you didn't find an attribute in the collection ? In that case you only have to modify the collection in order to retrieve the attribute...

例如,对于一种产品,magento提供了一种向您实例化的任何product-> collection自动添加(或删除)属性的方法. (请参阅Mage_Catalog的config.xml中的frontend/product/collection/attributes XML标签)

For a product for example, magento offer a way to automatically add (or remove) attributes to any product->collection you instantiate. (see frontend/product/collection/attributes XML tag in the config.xml of Mage_Catalog)