简体   繁体   English

Magento加载具有所有属性的产品集合

[英]Magento load product collection with all attributes

I need to iterate over some products and access custom attributes, my code is: 我需要遍历某些产品并访问自定义属性,我的代码是:

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(array('attribute' => 'status', 'eq' => '1')))
    // more filters
    ->load();

then I have to: 然后我必须:

foreach ($collection as $product) {
    // here I can't access custom attributes
    $product = Mage::getModel('catalog/product')->load($product->getId());
    // here I can access them
}

and my problem is when I have a lot of products my script spends a lot of time in foreach loop. 我的问题是当我有很多产品时,我的脚本在foreach循环中花费了很多时间。 Can I somehow speed this by loading collection with all attributes? 我可以通过加载具有所有属性的集合来以某种方式加快速度吗?

I suggest all your attributes should go in flat tables. 我建议您所有的属性都应放在平面表中。

So do some attributes setting like searchable, available in front end listing. 因此,某些属性设置(例如可搜索的属性)也会在前端列表中提供。

Then do an indexing. 然后做一个索引。

Now when you get product collection you will get needed attributes and then code from there. 现在,当您获得产品集合时,您将获得所需的属性,然后从那里进行编码。

Instead of a loop loading each product you need to load a collection of products. 无需循环加载每个产品,而是需要加载产品集合。

If you are using flat-tables, you have to add your custom attributes to the flat index. 如果使用平面表,则必须将自定义属性添加到平面索引。 You can add them in the config.xml of the according module. 您可以将它们添加到相应模块的config.xml中。 You can than skip the productload in the foreach which is probably causing the long processing time. 然后,您可以跳过foreach中的productload,这可能会导致较长的处理时间。

What you are doing is not a good practice and should be avoided. 您在做什么不是一个好习惯,应该避免。 You may try this 你可以试试这个

foreach($collection->getItems() as $item){

     echo $item->getName(); //$item is product instance. you can use it
}

EDIT 编辑

In your question, you are doing a serious mistake and it should be avoided. 在您的问题中,您犯了一个严重的错误,应该避免。 That is what I am trying to point out through my answer. 这就是我想通过我的答案指出的内容。

See what you are doing here. 在这里查看您正在做什么。

foreache($bigcollection as $singleone){
      $product = Mage::getModel('catalog/product')->load($id);
}

Basically you are loading a product inside a big for loop. 基本上,您是在big for循环中加载产品。 It is a big mistake. 这是一个大错误。 Because if you do this, Magento need to do "extra fetching works" from database. 因为如果这样做,Magento需要从数据库中进行“额外的提取工作”。 Do not use that method . 不要使用该方法 Through my solution, I showed you how to avoid loading a product inside a forloop. 通过我的解决方案,我向您展示了如何避免在forloop中加载产品。 It is much reliable method. 这是非常可靠的方法。

If you dont want to use forloop, I think you need to edit your question and put that requirement there. 如果您不想使用forloop,我认为您需要编辑您的问题并将该要求放在此处。

If you need to avoid for loop, you need to go for flat table concepts. 如果需要避免for循环,则需要使用平面表概念。 That is the only one way that I know. 那是我知道的唯一方法。 Otherwise you cannot avoid a for each loop. 否则,您无法避免每个循环都使用a。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM