$_category = Mage::getModel('catalog/category')->load($_category->getId()) 

$categories = Mage::getModel('catalog/category')
                    ->addAttributeToSelect(array('name', 'url'))
                    ->addFieldToFilter('is_topcategory', 1)
                    ->setPage(1, 5);


$subcats = $category->getChildren();
$CatCustomFilter = array();

if ($subcats) {
    foreach (explode(',', $subcats) as $subCatid) {
        $subcatergory = Mage::getModel('catalog/category')->load($subCatid);
        $subsubcats = $subcatergory->getChildren();
        if ($subsubcats) {
            foreach (explode(',', $subsubcats) as $subsubCatid) {
                $_subCategory = Mage::getModel('catalog/category')->load($subsubCatid);
                $CatCustomFilter[$subsubCatid] = $_subCategory->getCustomFilters();
        } else {
            $CatCustomFilter[$subCatid] = $subcatergory->getCustomFilters();

As you can see in FILE 1 Mage::getModel('catalog/category') is called twice. And in FILE 2 Mage::getModel('catalog/category') called in foreach loop.

So is there anyway to reduce this code? Because I think it's totally bad practice to load the Mage::getModel('catalog/category') like above.

Also please provide me some guidance on how to work with Mage::getModel() properly so that we can't call it again and again.


There is no need to use the load() method in the loop.

Magento provides a getChildrenCategories() method in Mage_Catalog_Model_Category class which in turn calls Mage_Catalog_Model_Resource_Category::getChildrenCategories().

So your file2 becomes something like:

$CatCustomFilter = array();
foreach ($category->getChildrenCategories() as $subcategory) {
    $subSubCategories = $subcategory->getChildrenCategories();
    if (count($subSubCategories)) {
        foreach ($subSubCategories as $subSubCategory) {
            $CatCustomFilter[$subSubCategory->getId()] = $subSubCategory->getCustomFilters();
    } else {
        $CatCustomFilter[$subcategory->getId()] = $subcategory->getCustomFilters();

as you can see, no load in the loops; the problem could be that custom_filters is a custom attribute of the category entity that wouldn't probably loaded; in that case you have to dig into the Mage_Catalog_Model_Resource_Category::_getChildrenCategoriesBase() method to see how to retrieve a category collection with custom attributes loaded.

provide me some guidance on how to work with Mage::getModel() properly

as you already spotted out, the problem is not related to using the Mage::getModel() but to using load in loops which is considered a worst practice in general.

Hope it helps!


You are completely right. There are better ways to do this more efficiently. Models interact with the resourcemodel, that interact with the database. It is right to use the model for such goal, however Magento provides us with a couple of functions that make this a little more efficient. Refer to these functions:

  • getChildren() returns a comma separated string of immediate children IDs
  • getAllChildren() returns a string or array of all children IDs
  • getChildrenCategories() returns a collection of immediate children categories


Also there is a design pattern that makes this usage of a models a bit more efficient. The Singleton design pattern makes sure there's only one instance. In above example you'll be spawning more than one instance, the singleton would be a good way too go while trying to implement one of above functions.

<?php $category = Mage::getSingleton('catalog/session'); ?>


If needed, i would happily provide you with some examples. But i think these references are enough for you to understand or where to search for. These concepts are the things you should know.

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.