1

我是 cakePHP 的新手,现在我必须进行分页。

评论表有一个 parent_id 并且线程查询工作正常,所以现在我想对结果进行分页。

我的问题是有限的 sql 查询会影响所有检索到的评论,我只想限制父评论,因为另一方面,它会将回复排除在查询之外。

希望我会很清楚,你可以帮助我。

谢谢。

4

2 回答 2

2

采用:

    var $hasMany = array(
            'ChildComment' => array(
                    'className' => 'ProfileComment',
                    'foreignKey' => 'parent_id',
                    'conditions' => '',
                    'dependent' => true,
                    'fields' => '',
                    'order' => 'created ASC'
            )
    );

        var $belongsTo = array(
            'ParentComment' => array(
                    'className' => 'ProfileComment',
                    'foreignKey' => 'parent_id',
                    'conditions' => '',
                    'fields' => '',
                    'order' => ''
            ));

然后在查找中:

$comments = $this->User->ProfileComment->find('all', array(
                'limit' => $this->incNumber,
                'offset' => $page*$this->incNumber,
                'conditions' => array('ProfileComment.parent_id' => null, 'ProfileComment.user_id' => $id),
                'order' => 'ProfileComment.created DESC'
            ));

您必须根据您的目的自定义代码,但关键是关系,并且查找条件具有 parent_id = null。这样限制只会影响父母

于 2011-04-25T09:45:24.087 回答
0

您可能只需要对父母(主题?)进行查询,然后对下面的每棵树进行另一个查询。

于 2010-01-26T12:15:14.510 回答