3

What is the difference between

$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();

and

<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>

Someone kindly explain.

5 Answers 5

4

There are 2 different objects.
A category model is the mapping of a model to the category tables.
A category has a many to many link to the products.
A category usually appears in the top navigation menu.

A layer model does not have a table behind it and it is in charge of the layered navigation.
A layer model has filters associated to it and one of the filters is the list of subcategories for the current category you are viewing.

A layer model does not need a category to display products. It is also used in search results.

3
  • so what should be done if we want to display the categories in layered navigation?
    – Ramya
    Commented Jun 21, 2016 at 7:56
  • 1
    That's a totally different question. and an unclear one.
    – Marius
    Commented Jun 21, 2016 at 7:58
  • As you mentioned A layer model does not need a category to display products am asking that whether $category = Mage::getSingleton('catalog/layer')->getCurrentCategory(); should not be used in the process to display categories in layered nav. kindly let me know if am still not clear.
    – Ramya
    Commented Jun 21, 2016 at 8:04
2

In my point of view, catalog/layer model is responsible for the filter criteria that you (commonly) see at left column in catalog pages

and catalog/category model is, say general model, used for most the operations to be done on category or by category

0
2

$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();

getCurrentCategory() is setter function of class Mage_Catalog_Model_Layer where magento set category for current layer naviation by which is used during layer navigation

For category page,magento is set current category as current layer navigation category(in which category products, magento create layer navigation)

For search page, it will set root category as layer navigation category on which Layer navigation is created

And <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>

Is load a category object basic current layer navigation category

0
1

Catalog layer works for layered navigation which you can find as filters in product view page.
The first code will return current category object on category page (product list) only.
The second line will load the category object for given ID which could be any id.

1
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();

You are get the same object over and over again throughout current loaded category.

<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>

You are get the newly created object again throughout pass category id.

1
  • I think, this is a different between getSingleton and getModel, NOT the LAYER and CATEGORY models Commented Jun 21, 2016 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.