1

Synopsis

I would like to create a category for Best Sellers, for this I create a root category which will contain certain products.

On my homepage, I have a block that will simply display these associated products.

The Problem

When I try to load my category model the normal way, I get an empty category model.

$category = Mage::getModel('catalog/category')->load(100);

So I tried the collection knowing this should work seeing as there wouldn't be any default filters:

$category = Mage::getResourceModel('catalog/category_collection')
    ->setIdFilter(100)
    ->getFirstItem();

Which works, with the exception it isn't what i'm looking for.

I found an article on StackOverflow describing one problem is that the category flat tables may not be indexed, so I ran the indexer as per:

$ php shell/indexer reindexall

Still, not having any luck with it. I checked the Admin configurations to double check flat tables were being used:

System > Configuration > Catalog > Frontend -> Use Flat Catalog Category

Where by Use Flat Catalog Category and Use Flat Catalog Product are both set to yes.

Inside my CMS Page I have implemented some layout XML following:

<block type="vendor/bestseller" name="home.best_seller" template="catalog/bestseller.phtml">
    <action method="setCategory"><category_id>193</category_id></action>
</block>

<?php

class Vendor_BestSeller_Block_Bestseller extends Mage_Core_Block_Template
{
    protected $category;

    public function setCategory($category)
    {
        if ( ! ($category instanceof Mage_Catalog_Model_Category)) {
            //$category = Mage::getResourceModel('catalog/category_collection')
            //    ->addIdFilter($category)
            //    ->getFirstItem();

            echo '<pre>';
            Zend_Debug::dump( $category );

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

            Zend_Debug::dump((string) $category->getCollection()->setSelect());
            die;
        }

        if ( ! $category->getId()) {
            Mage::throwException('Unable to load category.');
        }

        return $this->category = $category;
    }

    public function getCategory()
    {
        return $this->category;
    }
}
2

I found the solution to be simple but really frustrating.

When using flat tables each store has it's own flat category structure.

This means categories not associated to a store will never be accessible from the Model, only the resource collection (which uses the EAV structure).

Either use the resource collection:

Mage::getResourceModel('catalog/category')
    ->setIdFilter($id)
    ->getFirstItem();

Or associate the category to a store (or child of a root level category) and use the model:

Mage::getModel('catalog/category')->load($id);
| 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.