10
votes

Mage::getModel('catalog/category')->load($id) is not working (no data is loaded, and getId() is empty).

but if I do this:

     $collection = Mage::getModel('catalog/category')->getCollection();
     foreach($collection as $category) {
         if($category->getId() == $id) {
             echo "it has been founnnd!!!";

         }
     }

it works?

I should mention I am running this code through cli with phpunit tests so its bootstrapped. My guess is that there might be an issue with scope?

4

4 Answers

15
votes

I think the issue must be with your unit tests or somewhere else.

Mage::getModel('catalog/category')
  ->setStoreId($store_id)
  ->load($id);

works perfectly fine for me, no matter whether I use PHP's apache, apache2handler, cgi, cgi-fcgi or cli SAPI and no matter which Magento version (CE 1.3, 1.5, 1.6 and EE 1.11) I've tried.

5
votes

What I noticed (yes, after 3 years) is that:

1) Category will not load if you have category flat tables enabled and indexes are not up to date

2) Category will not load if it's not into "Default Category"

2
votes

Maybe flat category tables were used and index not up to date. See this article from alan storm.

2
votes

I hope it will definitely works,

$categoryId = 3;
$category = Mage::getModel('catalog/category')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->load($categoryId);