代码之家  ›  专栏  ›  技术社区  ›  Masoud92m

嵌套注释yii2

  •  1
  • Masoud92m  · 技术社区  · 7 年前

    我有一个评论表,如下所示:

    +-----------+--------------+------+-----+---------+----------------+
    | Field     | Type         | Null | Key | Default | Extra          |
    +-----------+--------------+------+-----+---------+----------------+
    | id        | int(11)      | NO   | PRI | NULL    | auto_increment |
    | user_id   | int(11)      | NO   |     | NULL    |                |
    | parent_id | int(11)      | NO   |     | 0       |                |
    | post_id   | int(11)      | NO   |     | NULL    |                |
    | body      | text         | NO   |     | NULL    |                |
    | date      | datetime     | NO   |     | NULL    |                |
    | status    | tinyint(1)   | NO   |     | 0       |                |
    +-----------+--------------+------+-----+---------+----------------+
    

    defautl的comment parent_id为0,如果回答comment,parent id将插入parent_id列。

    并用以下代码与用户表建立关系:

    public function getPosts()
    {
        return $this->hasMany(Post::className(), ['category_id' => 'id']);
    }
    

    如何显示嵌套?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Salem Ouerdani    7 年前

    首先,您需要定义 评论 模型类:

    public function getChildComments()
    {
        return $this->hasMany(self::className(), ['parent_id' => 'id']);
    }
    

    它定义了实体与自身的关系。我认为将相关的逻辑或处理程序保存在同一个类中的helper/callable方法中总是很好的,这样就不需要同时从数据库中加载它们。接下来要回答的问题是:

    如何显示嵌套?


    case01:在restful应用程序中

    简单地覆盖 fields() 里面 评论 类始终输出子注释:

    public function fields()
    {
        $fields = parent::fields();
        $fields['childs'] = 'childComments';
        return $fields;
    }
    

    就这样。 yii\rest\Serializer 应该注意递归表示,输出注释列表时会得到类似的结果:

    enter image description here


    case2:在html web视图中

    可能有很多方法可以实现它。我能想到的最简单、最干净的方法是绑定yii已经在使用的模板引擎,以递归方式重新呈现包含子注释的视图。作为一个工作示例,在 index.php索引 文件:

    <?php
    use yii\helpers\Html;
    use yii\widgets\ListView;
    use yii\data\ActiveDataProvider;
    use app\models\Comment;
    ?>
    
    <?= ListView::widget([
        'dataProvider' => new ActiveDataProvider([
            'query' => Comment::find(),
        ]),
        'itemView' => '_comment',
        'itemOptions' => ['style' => 'padding: 10px 50px; border: 1px solid red'],
    ]); ?>
    

    然后创建 _注释.php 文件:

    <?php
    use yii\helpers\Html;
    use yii\widgets\ListView;
    use yii\data\ActiveDataProvider;
    ?>
        <div style="background-color: skyblue; padding: 5px 15px">
            <h4><?= Html::encode($model->id) ?></h4>
            <p><?= Html::encode($model->name) ?></p>
        </div>
    
        <?php
            if ($model->getChildComments()->count()) {
                echo ListView::widget([
                    'dataProvider' => new ActiveDataProvider([
                        'query' => $model->getChildComments(),
                    ]),
                    'itemView' => '_comment',
                    'itemOptions' => ['style' => 'padding: 10px 0 10px 50px; border: 1px dotted blue'],
                ]);
            }
    
        ?>
    

    模板将在每次找到时创建自己的新实例 childComments 链接到所代表的那个。使用css填充来显示嵌套,代码应该输出:

    enter image description here