Recent Posts


Beware of Failed Object Loading


Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Number [god I don’t know] in the “Reasons Magento/PHP drives you Crazy” series.

Trying loading a product object that doesn’t exist.

$product = Mage::getModel('catalog/product')->loadByAttribute('sku','NO SUCH SKU');            

You’ll find $product contains NULL. Now try loading an attribute object that doesn’t exist.

$attribute = Mage::getModel('catalog/resource_eav_attribute')
->loadByCode('catalog_product','NO SUCH ATTRIBUTE');  

You’ll find that $attribute contains an unpopulated attribute object.

If you’re a Java/C# programmer, this drives you crazy because the loadByAttribute method should always return a product object (because methods have typed return values in those languages/system).

If you’re a PHP/Ruby/Python person you might expect to get NULL back from a method like this, but you’d expect Magento to behave consistently across object types. It doesn’t matter what the system does (returns NULL or returns an unpopulated object), it matters that the system behaves consistently when doing the same thing. (The same thing meaning instantiating an object)

Copyright © Alan Storm 1975 – 2021 All Rights Reserved

Originally Posted: 7th October 2013