Ajitha Ms
Ajitha Ms

Reputation: 555

Retrieve data from object - Magento

I am trying to get all the category ids and and their names in magento. For that, In our template file I have added following code:

$category = Mage::getModel('catalog/category')->getCollection()->getEntity();
echo '<pre>';
print_r($category);
echo '</pre>';

It returns following object.

 Mage_Catalog_Model_Resource_Category Object
    (
        [_tree:protected] => 
        [_categoryProductTable:protected] => catalog_category_product
        [_isActiveAttributeId:protected] => 
        [_storeId:protected] => 
        [_attributes:protected] => Array
            (
                [42] => Array
                    (
                        [value_id] => 3
                        [entity_type_id] => 3
                        [attribute_id] => 42
                        [store_id] => 0
                        [entity_id] => 2
                        [value] => 1
                    )

                [67] => Array
                    (
                        [value_id] => 4
                        [entity_type_id] => 3
                        [attribute_id] => 67
                        [store_id] => 0
                        [entity_id] => 2
                        [value] => 1
                    )
               .................. going on ....

From this object shall I get attribute_id? If can kindly explain me.. And also explain what are the things present in the collection, if you can. Thanks in advance.

Upvotes: 1

Views: 1030

Answers (1)

anz
anz

Reputation: 1042

try This:

$category = Mage::getModel('catalog/category')->getCollection();
foreach ($category as $cat)
{
  $catObj = Mage::getModel('catalog/category')->load($cat->getEntityId());

  //category Id
  var_dump($catObj->getEntityId());

  //category Name
  var_dump($catObj->getName());

}

Remember this loads all the categories (all categories created at the backend of any level and active/inactive).

you will need to filter as per your need. To do so you can filter at

$category = Mage::getModel('catalog/category')
                      ->getCollection()
                      //ADD YOUR FILTERS HERE
                      /*LIKE THESE
                      ->addIsActiveFilter()
                      ->addLevelFilter(1) */
                      ;

Upvotes: 3

Related Questions