Error on Mage::getModel(‘catalog/product’)->load($sku, ‘sku’) in Magento 1.7

magento-1.7modelproduct

when trying to load a product,

$foundProduct =  Mage::getModel('catalog/product')->load($sku, 'sku');

we get the following error:

Fatal error:
Uncaught exception 'Exception' with message 'Warning: Invalid argument supplied for foreach()
in app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 954' in app/code/core/Mage/Core/functions.php:245 Stack trace:
#0 app/code/core/Mage/Eav/Model/Entity/Abstract.php(954): mageCoreErrorHandler(2, 'Invalid argumen...', '/var/www/magent...', 954, Array)
#1 app/code/core/Mage/Catalog/Model/Resource/Abstract.php(698): Mage_Eav_Model_Entity_Abstract->load(Object(Mage_Catalog_Model_Product), '120x180 ES-bale...', 'sku')
#2 app/code/core/Mage/Core/Model/Abstract.php(225): Mage_Catalog_Model_Resource_Abstract->load(Object(Mage_Catalog_Model_Product), '120x180 ES-bale...', 'sku')
#3 updatemagento.php(197): Mage_Core_Model_Abstract->load('120x180 ES-bale...', 'sku')
#4 {main} thrown in app/code/core/Mage/Core/functions.php on line 245

Any ideas what's going wrong?

Best Answer

One option available to you would be to find the ID from the sku, and then load by ID.

$product = Mage::getModel('catalog/product');
$id = Mage::getModel('catalog/product')->getResource()->getIdBySku('your sku');
if ($id) {
    $product->load($id);
}