1

What happens when you load a non-existent product? i.e. when you call $product = Mage::getModel('catalog/product')->load('1'); and a product with id 1 does not exist in the database.

One would expect $product to be null; however, it is not. I check using the following conditional:

if (is_null($product)) {
    echo 'Product does not exist';
}

However, nothing is being printed. What is happening here? The debug console also tells me $product is of type "Simple". How can this be? The product with the id of 1 does not exist in my database.

3 Answers 3

2

You check it like this:

if (!$product->getId()) {

}
1

The product model shoudn't be a null value as well as any other model in Magento. To understand why lets see the logic of the load() method that you have used (actually the model loading logic consists in Mage_Core_Model_Resource_Db_Abstract::load() method):

public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
{
    if (is_null($field)) {
        $field = $this->getIdFieldName();
    }

    $read = $this->_getReadAdapter();
    if ($read && !is_null($value)) {
        $select = $this->_getLoadSelect($field, $value, $object);
        $data = $read->fetchRow($select);

        if ($data) {
            $object->setData($data);
        }
    }

    $this->unserializeFields($object);
    $this->_afterLoad($object);

    return $this;
}

As you can see the row values that were fetched saves to data array by setData() method. The ID value saves there as well as any other fetched data. There is one important rule: any row of the table has a unique identificator. So if the model hasn't contain a ID value, the model hasn't load or the required record hasn't existed in the database.

According to this, you can check if the model was loaded by the code snippet that has been provided in the first answer:

if ($product->getId()) {
    // product exists
} else {
    // product not found
}
1
  • I don't think this is correct as the example load() method you've used is a different implementation to the one in the question
    – scrowler
    Jan 31, 2016 at 22:13
0

To understand why it returns an uninitialized object, you should know how Mage::getModel('catalog/product')->load() actually works:

The following creates a new instance of Mage_Catalog_Model_Product (or whatever is rewriting the class:

$product = Mage::getModel('catalog/product');

When you call load() on it, this instance is populated with data from the database:

$product->load(1);

Now $product is initialized as the product with ID 1.

Conveniently, load() returns $this, so you don't have to assign the variable $product first and can chain the methods (as you did).

But it's not possible to unset an object from within itself, so $this can not be null. Instead, the product instance will still be uninitialized, after you called load($nonExistentId).

As it already has been pointed out, you can verify this by checking if $product->getId() is null.

Also, you get the product type "simple", because this is the default value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.