Hi everyone. It’s me again, David. Nice to meet you again in Magento blog! Today I’ll continue to accompany with you in the journey of discovering interesting things about Magento. In this tutorial, we will explore how Magento works with a category which helps customers easily find products that they need.
Category is understood as a group of products which have the same characteristics. I’ll introduce to you the category tree, the way Magento stores database of categories and how to use the category model in turn.
1. Category tree
Magento designs a category by tree structure. The category tree has a root category (the category without a parent category) and a subcategory (the child category of the parent category).
In Magento, a store associates with a root category. It makes you easily manage categories/products of each store. A category links to products assigned to it and the products of its child category. You can select these products in the tab Category Products in the backend.
To manage the category tree, Magento uses the model Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree which is extended from Varien_Data_Tree_Dbp. This model provides some methods to work with the tree structure such as: move a node, append s child, add a node …
2. Database storage
Magento stores database of categories by two types: EAV and Flat
In EAV, the category is stored in some tables:
The main table is catalog_category_entity. This table has some basic fields:
- parent_id: used to store the parent category. This value is zero (0) if the category is the root category
- path: the path to the category from the root category
- children_count: the number of children category of this category
When the category is stored in Flat database, a flat table catalog_category_flat_store_<StoreID> is created.
Please take a look at the table catalog_category_flat_store_1 below for example:
When the category is stored in Flat database, a flat table catalog_category_flat_store_<StoreID> is created.
Please take a look at the table catalog_category_flat_store_1 below for example:
3. How to use the category model
Magento provides the model and methods helping you work with categories by your code. For instance: you can use it to make the category displayed in the frontend. The default category model is Mage_Catalog_Model_Category including some important methods (you can view more in the source code of this model).
- getParentCategory(): this method returns the parent category of the current category
- getPathInStore(): the path of the current category that is calculated from the root category of the store (not includes the root category id)
- hasChildren(): detects the category to have children or not
- getCategories($parent, $recursionLevel = 0, $sorted=false, $asCollection=false, $toLoad=true): gets categories according to parents with the parameter that you provide
- getChildrenCategories: gets children categories of the current category
- …
Understanding how to work with categories in Magento is very practical for you to manage your store more effectively on your own initiative. Hope that my article is helpful for both store owners and developers 😉