首页 文章

Yii框架WP样式(使用WP db表)发布层次结构:我需要订购类别和子类别

提问于
浏览
0

我已经在Yii框架中启动了一个项目(Yii级别:初学者) . 我决定使用WP样式的db表来存储我的条目,并将它们分配给不同的类别,子类别,并为条目分配多个标签 .

所以基本上,这里是表格(请注意这里的名称是单数):

tbl_entry(entries)tbl_terms(name,slug of categories,subcategories,tags)tbl_term_taxonomy(category,subcategory,tag taxonomies)tbl_term_relationship(基于条目的ID,创建关系)

到目前为止我得到的是我能够获取它们(猫,子序列,标签) . 我也能分辨出类别和子类别,但我不能按层次排序 .

我需要他们:

Category 1
    Subcategory of Cat 1
    Another subcat of Cat 1
Category 2
    Subcategory of Cat 2
    Another subcat of Cat 2
    Yet another subcat of Cat 2
Catwegory 3
    etc.

目前,它们显示如下:

Another subcat of Cat 1
Category 2
    Subcategory of Cat 2
    Another subcat of Cat 2
Category 1
    Subcategory of Cat 1
    Another subcat of Cat 1
    Yet another subcat of Cat 2
Catwegory 3

关键在于:它们没有任何有意义的顺序显示,更准确地说,它们在被添加到数据库时被堆叠在一起 .

我不确定我是否应该能够分组,如果是,那么表和列是什么?

我在每个表的类中都有关系,并尝试将组子句放在那里,但没有成功 .

==================== E D I T ===============

我忘了陈述几件事 . 我正在尝试在显示时为单个条目进行此工作(显示猫,子类和标签) .

Controller EntryController
    public function actionView($id)
    {
        $entry = $this->loadModel();
        $relations = $entry->relations;

        $this->render('view', array(
            'entry' => $entry,
            'relations' => $relations,
        ));
    }

模型类Entry.php,其中 Build 了与Termrelationship.php的主要关系:

public function relations()
{
    return array(
        'relations' => array(self::HAS_MANY, 'Termrelationship', 'id',),
    );
}

view.php . 注意:因为它们只是关系(猫,子类,标签),并且来自关系表,它们没有在该表本身中设置任何层次结构 . 我必须比较分类法名称以给它们不同的标记(并将它们放在另一个块(标签)中) . Subcats在这里缩进:http://screencast.com/t/CvCBJoQqD0WP只是因为它们被包装在一个SMALL标签中 . 这很好,但我也希望能够列出主/父类别,然后列出该类别的所有子类,然后开始一个新的类别等...

<div class="row">
    <div class="col-md-6">

        <blockquote>
        <?php
            foreach($relations as $relation):
                if($relation->taxonomy['taxonomy']=='category'):
                    echo $relation->taxonomy->term->name . ' (Parent: '.$relation->taxonomy['parent'].')';
                endif;
                    if($relation->taxonomy['taxonomy']=='subcategory'):
                        echo '<small>'.$relation->taxonomy->term->name . ' (Parent: '.$relation->taxonomy['parent'].')</small>';
                    endif;
            endforeach;
        ?>
        </blockquote>

    </div>

    <div class="col-md-6">
        <?php
            foreach($relations as $relation):
                if($relation->taxonomy['taxonomy']=='tag'):
                    echo '<span class="label label-default">'.$relation->taxonomy->term->name.'</span> ';
                endif;
            endforeach;
        ?>
    </div>
</div>

1 回答

  • 0

    在查看relational active records的说明之后,您可以在每个模型的 relations() 函数中定义关系数据的正确 order 以及 with 定义以自动加载子项目 . 或者您可以使用 ->with(array('…'))->find() 操作手动将热切加载设置为自设程度 .

    // Entry actionView() Option one, manually setting depth
    // should load a single entry with relationships 2 levels deep + parent/current
    $entry = Entry::model()->with(array('relations.relations'))->findByPk($id); 
    
    // Entry Model, Option two fully specifying the relationship
    public function relations()
    {
        return array(
            'relations' => array(self::HAS_MANY, 'Termrelationship', 'id', 'order' => 'entry.name', 'with' => 'relations'),
        );
    }
    

    顺便说一句 . 你确定 relations() 中的 id 部分是正确的外键引用吗?您甚至可能希望将 through 修饰符用于关系以定义关于关系结构的其他信息 .

相关问题