Question

Maybe I'm missing something fundamental by how can it be that the following code works fine and finds an entity:

$model = Mage::getModel('catalog/category')->loadByAttribute('url_key', $urlKeyToCheck);

And the following is not:

$model = Mage::getModel('catalog/category');
$model->loadByAttribute('url_key', $urlKeyToCheck);
Was it helpful?

Solution

loadByAttribute returns an object, not related to the current model instance:

public function loadByAttribute($attribute, $value, $additionalAttributes = '*')
{
    $collection = $this->getResourceCollection()
        ->addAttributeToSelect($additionalAttributes)
        ->addAttributeToFilter($attribute, $value)
        ->setPage(1,1);

    foreach ($collection as $object) {
        return $object;
    }
    return false;
}

loadByAttribute does not work like load.
load changes the current model instance, loadByAttribute doesn't. It just returns the first object in the collection that has a specified value for a certain attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top