Magento : Mage::getModel clear data

magentomodelproduct

I have to develop a Magento plugin to export products.
I know other plugin already exist, but I must do it.

Export products is not really difficult, I use something like that (in condensed) :

public function __construct()
{
    $this->_productModel = Mage::getModel('catalog/product');

    //Other actions ..
}

public function _toHtml()
{
    $products = $this->_productModel->getCollection()
    ->addStoreFilter($storeId)
    ->addAttributeToFilter('type_id',array('in'=>$_types))
    ->joinTable('cataloginventory/stock_item', 'product_id=entity_id', array('qty'=>'qty','is_in_stock' => 'is_in_stock'), $this->_getStockSQL(), 'inner');

    foreach($product_list as $product)
    {
        $_p = $this->_productModel->load($product);
        $p = $_p->getData();

        //Export data, parse and return it ..
    }
}

So like you can see, I use a private var to stock Mage::getModel('catalog/product') object.
I thought to call this method each time consumed a lot of resources, that is what I wanted to store this variable.

But with this process, if a product is child of a configurable product, data are data are those of the parent.

So, can I clear this objet ?
Called Mage::getModel('catalog/product') he requires a lot of resources?
Can I force it to load the child data ?

Thanks 😉

Best Answer

Yes you can.

Take a look at the Mage_Catalog_Model_Convert_Adapter_Product for a standard example.

You'll see that they :

1) cache the product model :

public function getProductModel() {
  if (is_null($this->_productModel)) {
    $productModel = Mage::getModel('catalog/product');
    $this->_productModel = Mage::objects()->save($productModel);
  }
  return Mage::objects()->load($this->_productModel);
}

2) and reset it each time it's needed (at the beginning of the saveRow method) :

$product = $this->getProductModel()->reset();

Related Topic