Using the Magento Resource Iterator Model in a Product Collection

I recently found the following article regarding the use of the Magento resource modeler when invoking large product collections, to save resources - http://www.fontis.com.au/blog/magento/loading-large-collections . However, it’s hard for me to figure out how to get it to output the same data.

So, suppose I use the following to get a complete collection of products and display sku's:

$productCollection = Mage::getModel('catalog/product')->getCollection();
foreach($productCollection as $product){
    echo $product->getSku().',';
}

Using the profiler, I see that it uses 1,154,480 bytes for processing.

Now, using the resource iterator model, do the following:

function productCallback($args)
{
    $product = Mage::getModel('catalog/product');
        $product->setData($args['row']);
        echo $product->getSku().',';
}
$_productCollection = Mage::getModel('catalog/product')->getCollection();;
$_productCollection = Mage::getSingleton('core/resource_iterator')->walk($_productCollection->getSelect(), array('productCallback'));

In this case, 199,912 bytes are used. So much the difference to get the same, is simply sku's basic list.

, , , , URL- .., foreach $productCollection, :

foreach($_productCollection as $product){
echo $product->getSku().',';
};

. , , , foreach, .

, , , foreach? ? , ?

+2
3

, . , - , .

, , , , . , , , , , Magento . , .

+2

@clockworkgeek - . ,

$visibility = array(
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                  );

$_productCollection = Mage::getResourceModel('reports/product_collection')
                              ->addAttributeToSelect('*')
                              ->addOrderedQty()
                              ->addAttributeToFilter('visibility', $visibility)
                              ->setOrder('ordered_qty', 'desc');
+1

The question was asked some time ago, but I would like to add that you can load the product into a callback function and get any attribute you need and store it in an array of elements protected by objects using a public method.

For example, you might have a model like

class Company_Module_Model_Indexer_Entity_Product
     public function addToData($key, $data)
    {
        $this->_data[$key] = $data;
    }

then within the method of this class you can:

Mage::getSingleton('core/resource_iterator')->walk($products->getSelect(), array(
            function ($args) {
                $rowProduct = Mage::getModel('catalog/product')->setData($args['row']);
                $indexKey   = $rowProduct->getId();
                $product    = Mage::getModel('catalog/product')->load($rowProduct->getId());

                $args['invoker']->addToData($indexKey, [YOUR_DATA]);
            }
        ), array('invoker' => $this));
0
source

Source: https://habr.com/ru/post/1766390/


All Articles