Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Mage::getModel('catalog/category')->load($id) not working

Tags:

magento

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?

like image 203
Bryan Ruiz Avatar asked Mar 22 '12 10:03

Bryan Ruiz


2 Answers

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.

like image 91
Jürgen Thelen Avatar answered Oct 06 '22 20:10

Jürgen Thelen


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"

like image 27
Full Avatar answered Oct 06 '22 20:10

Full