php - Yii 框架 + WP 风格(使用 WP 数据库表)后层次结构 : I need to order categories and subcategories

标签 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

要点是:它们以无意义的顺序显示,更准确地说,它们在添加到数据库时被堆叠在另一个下面。

我不确定我是否应该能够对某些内容进行分组,如果可以,那么是什么表和列?

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

====================编辑===============

我忘了说明几件事。我目前正在尝试在显示时为单个条目执行此操作(显示猫、子猫和标签)。

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

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

与 Termrelationship.php 建立主要关系的模型类 Entry.php:

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

view.php。注意:由于它们都只是关系(猫、子猫、标签),并且来自关系表,因此它们在该表本身中没有设置任何层次结构。我必须比较分类名称以赋予它们不同的标记(并将它们放置在另一个 block (标签)中)。子目录在此处显示为缩进: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>

最佳答案

查看 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 修饰符来定义有关关系结构的附加信息。

关于php - Yii 框架 + WP 风格(使用 WP 数据库表)后层次结构 : I need to order categories and subcategories,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19669919/

相关文章:

MySQL 5.7 300 GB Innodb 性能问题

php - AJAX 提交表单的 Symfony2 Controller 访问错误

php - MySQL/PHP 数组到 Javascript

php - libphonenumber php 用法

mysql - 检查连接表中的记录是否多于一条

wordpress - 来自 Facebook 的 http 图片破坏了 https 证书

php - 直接在FTP上制作wordpress主题会不会更快?

wordpress - 在 wordpress 子主题中创建一个图像文件夹

php - 正则表达式不能与 PHP 一起正常工作,或者不知道如何实现它

php - 如何在php中连接xmpp?