3

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);
1
  • So did that answer your question? May 22 '14 at 17:38
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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