【问题标题】:Cakephp - Indented Related CommentsCakephp - 缩进相关评论
【发布时间】:2011-11-03 17:03:39
【问题描述】:

我在我的应用程序中创建了一个 cmets 系统,该系统现在附加到我的 articles->view。 cmets 控制器的功能之一是能够允许用户回复评论(一个级别)并能够为特定评论创建许多子 cmets。一切都通过 parent_id 连接。

现在,当我打开一篇文章时,我使用 foreach $comments as $comment 检索与文章相关的所有 cmets,并在 articles_controller.php 中将 $comments 变量分配给 find 与文章相关的所有 cmets。

获取$comment 的孩子cmets 并将其显示在我的视图上的最佳方法是什么?

我接近它的方法是有一个函数,它返回基于$comments->id 的子 cmets 列表并将其显示在$comment 下每下图...这似乎很耗时,因为调用将是为每条评论制作这个函数(即childrenComments($id)),绝对可以让服务器陷入困境。

有人知道使用parent_id 关系并节省一些CPU 时间的更好方法吗?

谢谢大家的帮助。

【问题讨论】:

    标签: cakephp-1.3


    【解决方案1】:

    如果它是“左”字段的嵌套集树顺序。如果您只有 parent_id,我认为按创建日期排序它们也应该有效,并获取与它们所属的外键相关的所有记录。

    【讨论】:

    • 我有 lft 字段和 rght 字段。但是,我的问题是如何做到这一点。我做了一些更多的研究,发现了 CakePHP 树行为,但是,我还没有思考如何完成它。我已经使用foreach $comments as $comment 来显示父 cmets。我如何从那里分支并显示每个父母的孩子 cmets?我稍后会尝试发布一些代码。另外,我在哪里添加ActsAs = array('Tree')?在ArticleComment 模型中?例如,来自Article 我使用$this->Article->Comment->id
    【解决方案2】:

    我不知道如何做到这一点,也许是正确的方法,所以我选择使用元素来完成这个。基本上,在foreach $comments 的每次迭代中,我都会调用childrenComments 元素来检查当前$comment['Comment']['id'] 是否有子cmets。如果有 cmets,我使用 for each 在其父评论下方显示每条评论。请参阅下面的一些代码......

    articles_controller.php 中的代码

    function childrenComments($id = null){
        $comments = $this->Article->Comment->find(
            'all',
            array(
                'fields' => array(
                    'Comment.id',
                    'Comment.comment',
                    'User.username',
                    'User.image'
                ),
                'conditions' => array(
                    'Comment.parent_id =' => $id,
                    'Comment.approved =' => 1
                )
            )
        );
        return $comments;
    }
    

    元素 childrenComments.ctp

    <div style="width:100%;">
    <div>
        <?php
        $childrenComments = $this->requestAction('/articles/childrenComments/'.$id);
        foreach ($childrenComments as $childrenComment){
        ?>
        <table cellpadding=0 cellspacing=0>
            <tr>
                <td>
                    <img style="width:30px;margin-right:5px" src="<?php echo $childrenComment['User']['image'];?>">
                </td>
                <td width=100% style="padding-left:10px;">
                    <?php
                        echo $childrenComment['Comment']['comment'];
                    ?>
                </td>
            </tr>
        </table>
        <?php
            }
        ?>
    </div>
    

    我的 /articles/view.ctp 中的代码

    <?php 
         foreach($comments as $comment){
     ...
     //Code are placed here to display each parent comment
         ...
    
             //This will iterate and display all children comments
             echo $this->element('childrenComments',array('id' => $comment['Comment']['id']));
         }
    ?>
    

    结果是:

    【讨论】:

      【解决方案3】:

      我找到了一个不使用 RequestAction 缩进 cmets 的解决方案。基本上,在视图中,我使用一个元素显示没有回复的 cmets。我将这个元素传递给整个 cmets 数组。在这个数组中,如果我们有一个“父 ID”的评论与我们正在显示的评论相匹配,那么就会有一个 foreach 再次循环,依此类推......

      在 Post/view.ctp 中:

      foreach ($post['Comment'] as $comment) {
         echo $this->element('comment', array('comments'=> $post['Comment'], 'id'=> $comment['id'], 'comment'=> $comment, 'user'=> $comment['User'], 'postId'=> $post['Post']['id'], 'level'=> '1'));
      }
      
      // Notice the whole comments array passed in the element 'comments'=> $post['Comment']
      

      在 Element/comment.ctp 中:

      // After the code that displays comment content
      
      foreach ($comments as $comm) {
        if ( $comm['parent_id'] == $comment['id'] ) {
          echo $this->element('comment', array('comments'=> $comments, 'id'=> $comm['id'], 'comment'=> $comm, 'user'=> $comm['User'], 'postId'=> $postId, 'level'=> $level));
        }
      }  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        • 2011-10-02
        • 2018-11-16
        • 2011-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多