我正在开发一些基于目录的项目,其中包含无限制的嵌套(实际上,最高级别为3,我猜,但它必须是动态的。)
让我说明做了什么:
OrganizationCategory模型:
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;
}
CategoryController:
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; ?>
您可以猜到,现在我只能获得那些仅与当前显示的类别相关联的产品。
我的目标是从类别及其所有子类别中获取产品 - &gt;子类别 - >子类别。
我试图在几乎所有地方找到解决方案,但仍然无法在我的项目中实现这个简单的任务。
感谢您提供的任何帮助,谢谢
答案 0 :(得分:0)
对于其他可能对此问题感兴趣的人,我已经解决了这个问题:
在类别控制器视图操作中,我调用:
$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;
}