CakePHP使用分页处理注释

CakePHP使用分页处理注释,cakephp,blogs,reply,Cakephp,Blogs,Reply,有没有一种方法可以使用树行为在cakePHP中对注释进行分页?我应该使用树行为还是自己编写代码来查看注释 对不起,以前有人问过这个问题。我发现一篇文章: …我会发布我的解决方案 不,我没有找到一个好的解决方案。我不想重新发明轮子,我只想用蛋糕的方式 我为递归打印注释编写了一个助手: # view/helpers/comments class CommentsHelper extends AppHelper{ public function printComments($comments = ar

有没有一种方法可以使用树行为在cakePHP中对注释进行分页?我应该使用树行为还是自己编写代码来查看注释

对不起,以前有人问过这个问题。我发现一篇文章: …我会发布我的解决方案

不,我没有找到一个好的解决方案。我不想重新发明轮子,我只想用蛋糕的方式

我为递归打印注释编写了一个助手:

# view/helpers/comments
class CommentsHelper extends AppHelper{
public function printComments($comments = array(), $params = array()){

    if (empty($comments) || !is_array($comments)) return false;

    echo '<ul id="comments-'.$comments[0]['Forum']['id'].'">';
    if (is_array($comments)){
        foreach($comments as $comment):
            ?>
            <li id="<?php echo $comment['Forum']['id']; ?>">
                <div class="inner">
                    <h4><?php echo $comment['Forum']['title']; ?></h4>
                    <div class="meta">
                        <?php echo $comment['User']['first_name']; ?>
                    </div>
                    <div class="content">
                        <?php echo $comment['Forum']['content']; ?>
                    </div>
                </div>
            <?php

            if (isset ($comment['children']))
            if (is_array($comment['children'])){
                if (!empty($comment['children'])) $this->printComments($comment['children']);
            }
            echo '</li>';

        endforeach;
    }
        else{
            echo '<li>'.$comment['Forum']['title'].'</li>';
        }
    echo '</ul>';
}
    $this->paginate = array(
            'recursive' => -1,
            'conditions' => array(
                'Comment.blog_id' => 0,
            ),
            'order' => array('parent_id', 'id'),
            'limit' => 10   
    );
    $this->set('comments', $this->paginate('Comment'));
问题是如何对评论进行分页


目前我使用两个表,一个用于根,第二个用于线程注释。我没有找到解决办法。

你不能。我所做的是在全线程注释视图和平面分页列表之间切换的链接。

你不能。我所做的是在全线程注释视图和平面分页列表之间切换的链接。

IMHO:你认为分页注释是个好主意吗?只需最小化页面上的空间注释,并将它们放在一个浮动中。不要让你的用户点击太多的链接…

IMHO:你认为分页评论是个好主意吗?只需最小化页面上的空间注释,并将它们放在一个浮动中。不要让你的用户点击太多的链接…

你必须通过分页在控制器中手动完成

关键是正确地排序mysql查询以查找注释,而不是按id,首先按父\ u id,然后按id(列仅为示例)-这将适用于2级注释:

# view/helpers/comments
class CommentsHelper extends AppHelper{
public function printComments($comments = array(), $params = array()){

    if (empty($comments) || !is_array($comments)) return false;

    echo '<ul id="comments-'.$comments[0]['Forum']['id'].'">';
    if (is_array($comments)){
        foreach($comments as $comment):
            ?>
            <li id="<?php echo $comment['Forum']['id']; ?>">
                <div class="inner">
                    <h4><?php echo $comment['Forum']['title']; ?></h4>
                    <div class="meta">
                        <?php echo $comment['User']['first_name']; ?>
                    </div>
                    <div class="content">
                        <?php echo $comment['Forum']['content']; ?>
                    </div>
                </div>
            <?php

            if (isset ($comment['children']))
            if (is_array($comment['children'])){
                if (!empty($comment['children'])) $this->printComments($comment['children']);
            }
            echo '</li>';

        endforeach;
    }
        else{
            echo '<li>'.$comment['Forum']['title'].'</li>';
        }
    echo '</ul>';
}
    $this->paginate = array(
            'recursive' => -1,
            'conditions' => array(
                'Comment.blog_id' => 0,
            ),
            'order' => array('parent_id', 'id'),
            'limit' => 10   
    );
    $this->set('comments', $this->paginate('Comment'));

您必须通过分页在控制器中手动执行此操作

关键是正确地排序mysql查询以查找注释,而不是按id,首先按父\ u id,然后按id(列仅为示例)-这将适用于2级注释:

# view/helpers/comments
class CommentsHelper extends AppHelper{
public function printComments($comments = array(), $params = array()){

    if (empty($comments) || !is_array($comments)) return false;

    echo '<ul id="comments-'.$comments[0]['Forum']['id'].'">';
    if (is_array($comments)){
        foreach($comments as $comment):
            ?>
            <li id="<?php echo $comment['Forum']['id']; ?>">
                <div class="inner">
                    <h4><?php echo $comment['Forum']['title']; ?></h4>
                    <div class="meta">
                        <?php echo $comment['User']['first_name']; ?>
                    </div>
                    <div class="content">
                        <?php echo $comment['Forum']['content']; ?>
                    </div>
                </div>
            <?php

            if (isset ($comment['children']))
            if (is_array($comment['children'])){
                if (!empty($comment['children'])) $this->printComments($comment['children']);
            }
            echo '</li>';

        endforeach;
    }
        else{
            echo '<li>'.$comment['Forum']['title'].'</li>';
        }
    echo '</ul>';
}
    $this->paginate = array(
            'recursive' => -1,
            'conditions' => array(
                'Comment.blog_id' => 0,
            ),
            'order' => array('parent_id', 'id'),
            'limit' => 10   
    );
    $this->set('comments', $this->paginate('Comment'));

您可以通过以下方式实现此目标:

您的模式代码:

var $hasMany = array(
    'Pages' => array(
        'className' => 'Page',
        'foreignKey' => 'parent_id',
    )
);
您的控制器代码:

$this->paginate = array(
        'Page' => array(
            'contain' => array(
                'Pages'
            ),
            'conditions' => array(
                'Page.parent_id' => null
            )
        )
    );
    $pages = $this->paginate();

您可以通过以下方式实现此目标:

您的模式代码:

var $hasMany = array(
    'Pages' => array(
        'className' => 'Page',
        'foreignKey' => 'parent_id',
    )
);
您的控制器代码:

$this->paginate = array(
        'Page' => array(
            'contain' => array(
                'Pages'
            ),
            'conditions' => array(
                'Page.parent_id' => null
            )
        )
    );
    $pages = $this->paginate();

您应该使用树行为和按lft排序。

您应该使用树行为和按lft排序。

如果我将使用两个表?一个用于根,第二个用于线程注释?也许我可以对根进行分页,但问题是要从数据库中获取数据。我不想将一个线程拆分为单独的页面。对线程注释进行分页是一个非常糟糕的主意,同时拆分表也是如此。但是你可以尝试制作可折叠/可扩展的线程。如果我使用两个表?一个用于根,第二个用于线程注释?也许我可以对根进行分页,但问题是要从数据库中获取数据。我不想将一个线程拆分为单独的页面。对线程注释进行分页是一个非常糟糕的主意,同时拆分表也是如此。但是,您可以尝试创建可折叠/可扩展的线程。每个评论都有标题和内容,用户将单击标题以查看内容。这将节省空间并将用户留在页面上。每个评论都有标题和内容,用户将单击标题以查看内容。这将节省空间并将用户留在页面上。