Yii 1.从类别及其子类别中获取产品[无限嵌套]
Yii 1. Get products from categories AND their subcategories [Unlimited nesting]
我正在做一些基于目录的项目,其中包含无限嵌套(实际上,我猜最大级别是 3,但它必须是动态的)。
让我展示一下做了什么:
组织类别模型:
public function relations()
{
return array(
'organizations' => array(self::HAS_MANY, 'Organization', 'organization_category_id'),
'parent' => array(self::BELONGS_TO, 'OrganizationCategory', 'category_parent_id'),
'children' => array(self::HAS_MANY, 'OrganizationCategory', 'category_parent_id'),
);
}
public static function getParentCategories()
{
if (is_null(self::$categories))
{
self::$categories = array();
$criteria = new CDbCriteria();
$criteria->order = 't.category_title';
$criteria->condition = "t.category_parent_id IS NULL";
$arr = self::model()->with('children')->findAll($criteria);
foreach ($arr as $item)
self::$categories[$item->primaryKey] = $item;
}
return self::$categories;
}
public static function createTree($children, $counter)
{
$tree = '';
if(count($children)){
$tree .= CHtml::tag('ul', array('class'=>'menu_open_'.$counter));
foreach($children as $child):
$childrenSub = self::createTree($child->children, $counter+1);
$tree .= CHtml::tag('li', (empty($childrenSub) ? array('class'=>'no_child_li') : array()));
$organizations = Organization::model()->findByAttributes(array('organization_category_id'=>$child->category_id));
$tree .= CHtml::link($child->category_title, array('organizationCategory/view', 'slug'=>$child->category_slug));
$tree .= $childrenSub;
$tree .=CHtml::closeTag('li');
endforeach;
$tree .= CHtml::closeTag('ul');
}
return $tree;
}
类别控制器:
public function actionView($slug = '')
{
$model = $this->loadModelSlug($slug);
$organizationsList = Organization::getOrganizations($model->category_id);
$this->pageTitle=$model->category_title;
Yii::app()->clientScript->registerMetaTag($model->meta_description, 'description');
Yii::app()->clientScript->registerMetaTag($model->meta_keywords, 'keywords');
$this->render('view',array(
'model'=>$model,
'organizationsList'=>$organizationsList,
));
}
组织模型 getOrganizations():
public static function getOrganizations($category_id) {
$criteria = new CDbCriteria;
$criteria->condition = 'organization_category_id = :cat_id';
$criteria->params = array(':cat_id'=>$category_id);
$result = self::model()->findAll($criteria);
return $result;
}
类别view.php
<?php if(!empty($model->children)) {
echo OrganizationCategory::model()->createTree($model->children, 1);
} ?>
<?php $i=1; foreach($model->organizations as $organization): ?>
blah blah blah
<?php $i++; endforeach; ?>
如您所料,现在我只能获取那些仅与当前显示的类别关联的产品。
我的目标是从类别及其所有子类别->子类别->子类别中获取产品。
我几乎到处都在寻找解决方案,但仍然无法在我的项目中实现这个微不足道的任务。
如有任何帮助,我将不胜感激,谢谢
对于其他可能对此问题感兴趣的人,我是这样解决的:
在我调用的类别控制器视图操作中:
$model = $this->loadModelSlug($slug);
$organizationsList = Organization::getOrganizationsFromSubcategories($model);
在组织模型中:
public static function getOrganizationsFromSubcategories($category) {
$childCategoriesIds = OrganizationCategory::getChildCategoriesIds($category);
$criteria = new CDbCriteria();
$criteria->addInCondition('organization_category_id', $childCategoriesIds);
$result = self::model()->findAll($criteria);
return $result;
}
最后,在 OrganizationCategory 中:
public static function getChildCategoriesIds($category)
{
$arr = array($category->primaryKey);
if (!empty($category->children))
foreach ($category->children as $child_category) $arr = array_merge($arr, self::getChildCategoriesIds($child_category));
return $arr;
}
我正在做一些基于目录的项目,其中包含无限嵌套(实际上,我猜最大级别是 3,但它必须是动态的)。
让我展示一下做了什么:
组织类别模型:
public function relations()
{
return array(
'organizations' => array(self::HAS_MANY, 'Organization', 'organization_category_id'),
'parent' => array(self::BELONGS_TO, 'OrganizationCategory', 'category_parent_id'),
'children' => array(self::HAS_MANY, 'OrganizationCategory', 'category_parent_id'),
);
}
public static function getParentCategories()
{
if (is_null(self::$categories))
{
self::$categories = array();
$criteria = new CDbCriteria();
$criteria->order = 't.category_title';
$criteria->condition = "t.category_parent_id IS NULL";
$arr = self::model()->with('children')->findAll($criteria);
foreach ($arr as $item)
self::$categories[$item->primaryKey] = $item;
}
return self::$categories;
}
public static function createTree($children, $counter)
{
$tree = '';
if(count($children)){
$tree .= CHtml::tag('ul', array('class'=>'menu_open_'.$counter));
foreach($children as $child):
$childrenSub = self::createTree($child->children, $counter+1);
$tree .= CHtml::tag('li', (empty($childrenSub) ? array('class'=>'no_child_li') : array()));
$organizations = Organization::model()->findByAttributes(array('organization_category_id'=>$child->category_id));
$tree .= CHtml::link($child->category_title, array('organizationCategory/view', 'slug'=>$child->category_slug));
$tree .= $childrenSub;
$tree .=CHtml::closeTag('li');
endforeach;
$tree .= CHtml::closeTag('ul');
}
return $tree;
}
类别控制器:
public function actionView($slug = '')
{
$model = $this->loadModelSlug($slug);
$organizationsList = Organization::getOrganizations($model->category_id);
$this->pageTitle=$model->category_title;
Yii::app()->clientScript->registerMetaTag($model->meta_description, 'description');
Yii::app()->clientScript->registerMetaTag($model->meta_keywords, 'keywords');
$this->render('view',array(
'model'=>$model,
'organizationsList'=>$organizationsList,
));
}
组织模型 getOrganizations():
public static function getOrganizations($category_id) {
$criteria = new CDbCriteria;
$criteria->condition = 'organization_category_id = :cat_id';
$criteria->params = array(':cat_id'=>$category_id);
$result = self::model()->findAll($criteria);
return $result;
}
类别view.php
<?php if(!empty($model->children)) {
echo OrganizationCategory::model()->createTree($model->children, 1);
} ?>
<?php $i=1; foreach($model->organizations as $organization): ?>
blah blah blah
<?php $i++; endforeach; ?>
如您所料,现在我只能获取那些仅与当前显示的类别关联的产品。
我的目标是从类别及其所有子类别->子类别->子类别中获取产品。
我几乎到处都在寻找解决方案,但仍然无法在我的项目中实现这个微不足道的任务。
如有任何帮助,我将不胜感激,谢谢
对于其他可能对此问题感兴趣的人,我是这样解决的:
在我调用的类别控制器视图操作中:
$model = $this->loadModelSlug($slug);
$organizationsList = Organization::getOrganizationsFromSubcategories($model);
在组织模型中:
public static function getOrganizationsFromSubcategories($category) {
$childCategoriesIds = OrganizationCategory::getChildCategoriesIds($category);
$criteria = new CDbCriteria();
$criteria->addInCondition('organization_category_id', $childCategoriesIds);
$result = self::model()->findAll($criteria);
return $result;
}
最后,在 OrganizationCategory 中:
public static function getChildCategoriesIds($category)
{
$arr = array($category->primaryKey);
if (!empty($category->children))
foreach ($category->children as $child_category) $arr = array_merge($arr, self::getChildCategoriesIds($child_category));
return $arr;
}