1
votes

Magento Product data in Mage::getSingleton('catalog/product') not clearing if product id not available in database

Eg:

Product ID - 586 (Poduct present)

Product ID - 999 (Product not present in Magento)

Not Working:

    $productModel = Mage::getSingleton('catalog/product');
    $_product=$productModel->load('586');
    echo $_product->getId()."<br>";

    $productModels = Mage::getSingleton('catalog/product');
    $_products=$productModels->load('999');
    echo $_products->getId()."<br>";

Output:

586

586

Working:

    $productModel = Mage::getSingleton('catalog/product');
    $_product=$productModel->load('999');
    echo $_product->getId()."<br>";

    $productModels = Mage::getSingleton('catalog/product');
    $_products=$productModels->load('586');
    echo $_products->getId()."<br>";

Output:

586

Anyone please help!..

2

2 Answers

0
votes

You can use Mage::getModel() instead of Mage::getSingleton().

<?php
$productModel = Mage::getModel('catalog/product');
$_product=$productModel->load('586');
echo $_product->getId()."<br>";

$productModels = Mage::getModel('catalog/product');
$_products=$productModels->load('999');
echo $_products->getId()."<br>";
?>

Output 
586

Model create new instance when Singleton use original instance.

0
votes

You can clear the instance in between.

$_product->clearInstance();