![](/img/trans.png)
[英]Magento get child categories / category collection handling. Programming efficiency
[英]Get Child categories magento
试图获得活跃的特定类别的孩子。 请帮忙。 我无法做到这一点。 我现在能够向他们展示所有但不具体。 非常感谢任何帮助。
$category = Mage::getModel('catalog/category')->load(2);
$category->getChildCategories();
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
这是加载活动类别的代码
/* Load category by id*/
$cat = Mage::getModel('catalog/category')->load($id);
/*Returns comma separated ids*/
$subcats = $cat->getChildren();
//Print out categories string
#print_r($subcats);
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive())
{
$caturl = $_category->getURL();
$catname = $_category->getName();
if($_category->getImageUrl())
{
$catimg = $_category->getImageUrl();
}
echo '<h2><a href="'.$caturl.'" title="View the products for this category"><img src="'.$catimg.'" alt="" />'.$catname.'</a></h2>';
}
}
?>
希望这对你有所帮助。
正如mhaupt所提到的,加载集合而不是循环中的每个类别更快。 但是,就我而言,没有必要手动加载子类别。 基本上这就是$category->getChildrenCategories()
已经做到的。
还有一个过滤器只能获取活动类别。 只需在集合上调用addIsActiveFilter()
。
a。)通过getChildren()
加载活动的子类别
// 1. Get a list of all child category ids (e.g "12,23,11,42")
$subcategoryIds = $category->getChildren();
// 2. Create collection
$categoryCollection = Mage::getModel('catalog/category')->getCollection();
// 3. Add all attributes to select, otherwise you can not
// access things like $cat->getName() etc.
$categoryCollection->addAttributeToSelect('*');
// 4. Filter by ids
$categoryCollection->addIdFilter($subcategoryIds);
// 5. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();
b。)使用getChildrenCategories()
加载活动子类别
// 1. Load collection
$categoryCollection= $category->getChildrenCategories();
// 2. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();
一旦访问该集合,就会从数据库中加载该集合。 如果未加载集合并且调用$subcategories->count()
则仅针对数据库触发“SELECT count(*)”(与count($subcategories)
相反,这将强制集合加载自身)。
迭代集合
foreach($categoryCollection as $category) {
echo $category->getName();
}
如果在访问集合后向集合添加更多过滤器,则集合将不会自动再次加载。 要将更改应用于集合,只需调用$categoryCollection->load()
从数据库重新加载集合。
那些说使用getAllChildren()而不是getChildren()的人完全错了。 两种方法都返回完全相同的东西,但有一点不同,getAllChildren(true)将返回一个数组而不是逗号分隔的字符串。 getAllChildren($ bool asArray)默认为false。 我的观点是,无论哪种方式,你都必须使用
Mage::getModel('catalog/category')->load($catId);
除非你使用下面的函数,否则在循环内部。
private function fetchCatsById($onlyThese)
{
$cats = Mage::getModel('catalog/category')
->getCollection(true)
->addAttributeToSelect('*')
->addIdFilter($onlyThese)
->addAttributeToFilter('level','2')
->addIsActiveFilter();
return $cats;
}
$cats = $this->fetchCatsById($onlyThese);
liyakat写的一个答案,不应该在专业商店中使用,因为它会引发性能问题,因为类别对象有多次n次加载,而是使用类别集合,让所有孩子
$cat->getAllChildren()
,然后通过所需的类别ID限制类别集合
$coll->addIdFilter($idFilter);
那么您将不必对数据库加载n次。
请记住,循环中的加载是任何Magento项目中最常用的错误代码示例之一,并避免它们!
你好,你会看到下面的代码
$category_model = Mage::getModel('catalog/category');
$_category = $category_model->load(13);
$all_child_categories = $category_model->getResource()->getAllChildren($_category);
print_r($all_child_categories);
如果你想要任何数量的父类别子类别而不是点击这里http://magentoo.blogspot.com/2014/01/get-all-subcategories-of-parent-category-magento.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.