Phil Birnie
Phil Birnie

Reputation: 1134

Fatal Error when calling getChildrenCategories on a Flat Category Collection in Magento

Basically, I'm trying to display a list of the URL, thumbnail, and name of the top categories in our Magento store in a (core/template) block on our home page. We're using Magento Enterprise 1.12.0.2, though I think the code referenced would also apply to the Community Edition.

To Load the Categories I'm using the following:

/**
 * Parent Category of store
 * @var Mage_Catalog_Model_Category
 */
$_parent_category = Mage::getModel('catalog/category')->load(2);

/**
 * Resource Model that will allow us to load the Categories.
 * @var Mage_Catalog_Model_Resource_Category
 */
$_resource = Mage::getResourceModel('catalog/category');

/**
 * Collection of Child Categories
 * @var Mage_Catalog_Model_Resource_Category_Collection
 */
 $_categories = $_resource->getChildrenCategories($_parent_category);

Everything works fine until I switch to flat tables (change System -> Configuration -> Catalog -> Frontend -> Us Flat Catalog Category) to "Yes"

Once I do that, I get the following error:

Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Category_Flat_Collection::joinUrlRewrite() in /app/code/core/Mage/Catalog/Model/Resource/Category.php

I reindexed, flushed all caches, etc..

Is this a bug in the core code base or simply an execution error on my part?

Thanks in advance for any guidance.

Upvotes: 1

Views: 2701

Answers (1)

Marius
Marius

Reputation: 15216

This happens because the category model (catalog/category) uses different resource models for different settings of Use Flat Catalog Category and you are always using Mage::getResourceModel('catalog/category');. If the flat categories are enabled the resource model should be Mage::getResourceModel('catalog/category_flat');.
In order not to be bothered by this I recommend using

$_parent_category = Mage::getModel('catalog/category')->load(2);
$_categories = $_parent_category->getChildrenCategories();

and let Magento decide what resource model to use.

Upvotes: 3

Related Questions