0

Please we need some help with magento 1.8 On custom template we have categories list menu on left side. If we activate Flat catalog categories we have memory error by getting category list.

Here the code works for 1.7 and not for 1.8

<div class="block-content">
<?php

$rootcatId= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
function  get_categories($categories) {
    $array= '<ul>';
    foreach($categories as $category) {
        $cat = Mage::getModel('catalog/category')->load($category->getId());
        $array .= '<li>'.
        '<a href="' . Mage::helper('core/url')->getHomeUrl().$cat->getUrlPath(). '">' .
        $category->getName() . "</a>\n";
        if($category->hasChildren()) {
            $children = Mage::getModel('catalog/category')->getCategories($category->getId());
            $array .=  get_categories($children);
        }
        $array .= '</li>';
    }
    return  $array . '</ul>';
}
echo  get_categories($categories); ?>

</div>

1 Answer 1

1

Looks like you're building a nested navigation. Magento does this by default; though I understand your pain. Maybe the following will help:

A few issues here:

  • Your $array var is a string. Not an array. Rename it!
  • You are defining a function in a view file. This is a bad practice. Move this logic to the block and don't use first-class functions. Rather use a method on the block, and enact is as $this->getCategories($categories).*
  • You are loading models from the view. Another bad practice, but moreso because every getModel makes successive calls to the database. Use a collection instead.

The benefit to moving your logic out of the view is that block output is cached, view output is not (although FPC is a cache in Enterprise Edition, but we'll ignore that for now as it opens a can of worms).

Moving on to actually solving your problem, though - I just verified with your actual code - you do not need to load categories differently depending on whether the flat catalog is enabled or not. You do need to ensure that your flat category indexes are totally refreshed. The flat indexers do not show up in your index configuration panel unless the flat catalogs are enabled.

You can reindex the flat catalogs in System > Index Management. You can also reindex from the command line from your Magento web root with the following command:

$ php shell/indexer.php reindexall

enter image description here

More information on reindexing with regard to 1.8:

http://www.magentocommerce.com/knowledge-base/entry/ce-18-indexing


* If we're getting really proper, don't pass the $categories into it from the view. Have a method in the block that retrieves the appropriate categories for you.

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.