Need help understanding Magento product collection objects and the catalog/product model

≡放荡痞女 提交于 2019-12-24 07:54:43

问题


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?

I have code like this:

<?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 ?>

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.).

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) Why are the above objects so huge?

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) Is there a performance or memory hit from using objects this way?

Many thanks!


回答1:


You have several ways to access data from magento :

  • 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)

  • 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

  • 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

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...

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)




回答2:


It's becouse magento use cache recursive OMG system and make relational database developers crazy. I'm one of those. But, by my understanding i get it's better use Mage::getModel('model')->getCollection(), then some king of sql search with php becouse when Magento store starts it make a biggggggg cache schema to suply some info faster, it's a kind of indexring.

So, if you want just few fields you can use .....->getCollection()->addAttributeToSelect('name'). For ex.

Gl on your magento understanding.



来源:https://stackoverflow.com/questions/12936989/need-help-understanding-magento-product-collection-objects-and-the-catalog-produ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!