dongtan6206 2012-10-17 14:46
浏览 22
已采纳

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

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!

  • 写回答

2条回答 默认 最新

  • dongluan2612 2012-10-17 15:37
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等