CakePHP的高效分页

时间:2011-07-27 16:02:16

标签: cakephp pagination

我对CakePHP有一个“架构”问题:p。

我必须对查询进行分页,这看起来很简单,我使用$ paginate数组和paginate方法,但我有很多限制。

在我的控制器的许多方法中,我必须返回相同模型的不同字段,在这两种情况下我都必须分页。这个事实迫使我提到$ paginate数组中的所有字段,当我不需要那些字段时,这可能会导致性能不佳。

如何以干净的方式为不同的方法设置不同的分页规则?

(我想在使用不同的数组并在运行时将$ paginate分配给特定的数组,但我想知道是否有一种“官方方式”来实现它)

2 个答案:

答案 0 :(得分:1)

如果你的表没有大量的字段,我认为让蛋糕查询所有字段是可以的。表现应该差别不大。

您可以指定不同的分页集:

var $paginate = array(
   'Recipe' => array (...),
   'Author' => array (...)
);

然后$data = $this->paginate('Recipe');

http://book.cakephp.org/view/1232/Controller-Setup

答案 1 :(得分:0)

我不知道这是否是你已经在做的事情,但我认为这已经足够干净了:

function foo() {
    $this->paginate['fields'] = array('field_1', 'field_2');
    /* rest of the method */
}
function bar() {
    $this->paginate['fields'] = array('field_3', 'field_4');
    /* rest of the method */
}

如果您将在所有方法中使用字段,您也可以这样做:

var $paginate  = array (
    'fields' => array('always_need_this', 'also_need_this_always',)
);
function foo() {
    array_push($this->paginate['fields'], 'only_in_foo', 'also_only_in_foo');
    /* rest of the method */
}
function bar() {
   array_push($this->paginate['fields'], 'only_in_bar', 'also_only_in_bar');
    /* rest of the method */
}