CakePHP分页最大限制不起作用

时间:2015-04-29 07:48:01

标签: php cakephp

我正在使用cakephp 2.x.我无法设置记录的最大限制。

请检查我的代码:

App::uses('AppController', 'Controller');
class BroadcastsController extends AppController {
   public $components = array('Paginator');

   public function broadcast(){           
         $this->Paginator->settings = array('limit' => 10,  'order' => array('Broadcast.no_of_user' => 'DESC'), 'group' => 'Broadcast.broadcaster_id');
         $popularRooms = $this->Paginator->paginate('Broadcast');
         pr($popularRooms); //fetch 200 records
         $this->set('popularRooms', $popularRooms);
   }
}

高于pr($popularRooms);我有200条记录,但我想要前50条记录,每页显示10条记录。我使用的是'maxLimit'=>50,但此代码与'limit'=>50相同。请帮帮我。

5 个答案:

答案 0 :(得分:4)

在这种情况下,您可以在分页器设置中使用'extras'数组

$this->Paginator->settings = array('limit' => 10, 'max_record'=>50, 'order' => array('Broadcast.no_of_user' => 'DESC'), 'group' => 'Broadcast.broadcaster_id');
模型中的

或覆盖paginateCount函数的AppModel

class Broadcast extends AppModel { 
function paginateCount($conditions, $recursive, $extra) {
    $param = array_merge(compact('conditions', 'recursive'), $extra);
    $count = $this->find('count', $param);
    if (!empty($extra['max_record']) && $count > $extra['max_records']) {
      $count = $extra['max_record'];
    }
    return $count;
  } 
}

答案 1 :(得分:1)

默认情况下,CakePHP将可获取的最大行数限制为100.您可以将其作为分页选项的一部分进行调整,如下所示

public $paginate = array(
    // other keys here.
    'maxLimit' => 10
);

对于你的代码,设置maxLimit就像这样

 $this->Paginator->settings = array('limit' => 10,'maxLimit'=>50,'order' => array('Broadcast.no_of_user' => 'DESC'), 'group' => 'Broadcast.broadcaster_id');

答案 2 :(得分:0)

public function list_posts() {
    $settings = array(
       'limit' => 25, // here
       'order' => array(
          'Post.title' => 'asc'
        )
    );
    $this->Paginator->settings = $this->settings;
    // similar to findAll(), but fetches paged results
    $data = $this->Paginator->paginate('Posts');
    $this->set('data', $data);

}

答案 3 :(得分:0)

我相信你不必拥有$ this-> Paginator-> settings =。

public function list_posts() {
$settings = array(
   'limit' => 25, // here
   'order' => array(
      'Post.title' => 'asc'
    )
);
// similar to findAll(), but fetches paged results
$data = $this->Paginator->paginate('Posts');
$this->set('data', $data);

答案 4 :(得分:0)

在限制之前先在您的分页器设置中设置maxLimit,如下所示:

$this->Paginator->settings = array(
 'maxLimit' => 50,  
 'limit' => 10,  
 'order' => array('Broadcast.no_of_user' => 'DESC'), 
 'group' => 'Broadcast.broadcaster_id'
);

只在逻辑上设置其中一个不会改变任何内容。