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

时间:2013-10-29 21:54:28

标签: php mysql wordpress yii

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

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

tbl_entry(条目) tbl_terms(名称,类别,子类别,标签) tbl_term_taxonomy(类别,子类别,标记分类法) 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,其中建立了与Termrelationship.php的主要关系:

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

view.php。注意:因为它们只是关系(猫,子类,标签),并且来自关系表,它们在该表本身中没有设置任何层次结构。我不得不比较分类法名称给它们不同的标记(并将它们放在另一个块(标签)中)。子标记在此处缩进: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 :(得分:0)

在查看relational active records的说明后,您可以在每个模型的order函数中为关系数据定义正确的relations()以及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'),
    );
}    

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