1

When I run

$menu = array(
                'items' => array(),
                'parents' => array()
            );
            $category = Mage::getModel('catalog/category')->getCollection();
            $category->addAttributeToFilter("Top_Menu" , array('eq' => '146'));
            $category->addAttributeToSelect('name','parent_id','entity_id','url');
            $category->setOrder("parent_id", "ASC");
            $category->load();

            foreach($category as $category1):
            $idcat=$category1->getId();
            $parentcat=$category1->getParentId();
            $menu['items'][$idcat][] = $category1->getUrl();
            $menu['items'][$idcat][] = $category1->getName();
            $menu['items'][$idcat][] = $parentcat;
            $menu['parents'][$parentcat][] = $idcat;
            endforeach;

I get a lot of queries in my db

but when when run

$menu = array(
                'items' => array(),
                'parents' => array()
            );
            $category = Mage::getModel('catalog/category')->getCollection();
            $category->addAttributeToFilter("Top_Menu" , array('eq' => '146'));
            $category->addAttributeToSelect('name','parent_id','entity_id','url');
            $category->setOrder("parent_id", "ASC");
            $category->load();

Its much less

Why is that? how do I avoid the "get" functions.

2

Most of these will be coming from the getUrl since the url is not a feild in this collection and it is really calling a function rather than the magic getter method.

Mage_Catalog_Model_Category

public function getUrl()
    {
        return $this->getUrlModel()->getCategoryUrl($this);
    }

If you want to be more confident that it's not calling a function which itself calls more mysql you can use getData('') instead e.g getData('name') instead of getName().

| improve this answer | |
  • If your url follows a standardized template it is possible to derive the url from other methods (setting a variable to the root url and appending the $ID and / or $Name, for example). – hownowbrowncow Nov 17 '14 at 19:49
0
$category = Mage::getModel('catalog/category')->getCollection();
    $category->addAttributeToFilter("Top_Menu" , array('eq' => '146'));
    $category->addAttributeToSelect('*');
    $category->setOrder("parent_id", "ASC");
    $categoryArray = $category->toArray();
| improve this answer | |

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.