CakePHP 1.3.6 中的分页+路由问题

问题描述 投票:0回答:1

如果我从路由网址访问结果,则无法对结果进行分页。这些是我正在使用的路线:

// NEWS
Router::connect('/news.rss', array('controller' => 'posts', 'action' => 'index', 'ext' => 'rss'));
Router::connect('/news/*', array('controller' => 'posts', 'action' => 'index'));
Router::connect('/:lang/posts/*', array('controller' => 'posts', 'action' => 'index'));

我知道在最后一条路线中我没有传递 :lang 参数,但如果我传递它:

Router::connect('/:lang/news/*', array('controller' => 'posts', 'action' => 'index'), array('lang' => $regex['lang'], 'pass' => array('lang')));

也不起作用。

如果我尝试访问 url /news/page:2 它会显示第一页的结果。我打印了 $this->params 来查看它是否正确地获取页码,并且,在第一个实例中,它确实如此:

Array
(
[lang] => ca
[named] => Array
    (
        [page] => 2
    )

[pass] => Array
    (
    )

[controller] => posts
[action] => index
[plugin] => 
[url] => Array
    (
        [ext] => html
        [url] => ca/posts/page:2
    )

[form] => Array
    (
    )
[...]
)

如果我访问 /news/page:2 和 /posts/index/page:2,数组的这一部分(我省略了一些部分,稍后将向您展示)是相同的,但是如果您看一下这部分:

Array
(
[...]
[paging] => Array
    (
        [Post] => Array
            (
                [page] => 1
                [current] => 3
                [count] => 3
                [prevPage] => 
                [nextPage] => 
                [pageCount] => 1
                [defaults] => Array
                    (
                        [limit] => 3
                        [step] => 1
                        [order] => Post.created DESC
                        [conditions] => Array
                            (
                                [Post.active] => 1
                                [Post.page] => 
                                [0] => Post.public_date <= NOW()
                            )

                    )

                [options] => Array
                    (
                        [page] => 1
                        [limit] => 3
                        [order] => Post.created DESC
                        [conditions] => Array
                            (
                                [Post.active] => 1
                                [Post.page] => 
                                [0] => Post.public_date <= NOW()
                            )

                    )

            )

    )

您可以看到它没有正确获取页码。但如果我从 /posts/index/page:2 访问,它会很好地获取数字并且分页有效。

如果漂亮的网址就不会打扰我,但考虑到该网站是多语言的,如果我访问 /en/posts/index/page:2 (或 /en/news/page:2),我至少需要它可以工作...

这是我的完整的routes.php 文件:

http://pastebin.com/th4hLZNz

有人知道发生了什么吗?

提前致谢

php routes cakephp pagination cakephp-1.3
1个回答
0
投票

终于找到解决办法了。其实这很简单,最大的问题是我没有集中注意力。我专注于路线,以为自己做错了什么,但问题出在控制器上。

问题是这样的:

$this->paginate['conditions'] = array(
    'Post.active' => true,
    'Post.page' => $page,
    'Post.public_date <= NOW()');

$this->paginate['group'] = 'Post.id';

该组(在符合条件的情况下)不允许 paginateCount 函数很好地计算结果。所以我在 Post 模型上创建了自己的 paginateCount 函数,缺少组条件:

/**
 * This method fixes the count query 
 * when there's a GROUP BY on it
 *
 * @return integer
 */
public function paginateCount($conditions = null, $recursive = 0, $extra = array())
{
    $parameters = compact('conditions', 'recursive');
    $params = array();
    if (isset($extra['group']))
    {
        $params = array_merge(array(
            'fields' => array(
                'COUNT(DISTINCT(' . $this->alias . '.' . $this->primaryKey . ')) AS `count`'
            )
        ), $parameters);
    }
    else
    {
        $params = array_merge($parameters, $extra);
    }
    return $this->find('count', $params);
}

现在看来效果很好

© www.soinside.com 2019 - 2024. All rights reserved.