CakePHP 3.x - How to pass pagination configuration directly instead of using the controller `$paginate` property?

↘锁芯ラ 提交于 2021-01-28 19:57:45

问题


The below code works:

// Somewhere in the Controller
public $paginate = [ 'maxLimit'=>2 ];
// In the method:
$query=$this->Model->find('all')->where(....);
$this->set('results',$this->paginate($query));

However, I do not want to specify $paginate as public in the controller. I would rather not specify it at all. I tried to move maxLimit setting to the method but I'm doing it incorrectly. How can I change the below code?

$query=$this->Model->find('all')->where(....);
$this->set('results',$this->paginate($query, ['maxLimit'=>2]));

回答1:


The Controller::paginate() method doesn't take a second argument. What you are looking for is the paginate() method of the Paginator component, which is accessible in your controller via the $this->Paginator property.

$this->Paginator->paginate($query, ['maxLimit' => 2])

See also

  • Cookbook > Controller > Components > Pagination > Using the Paginator Directly
  • API > \Cake\Controller\Component\Paginator::paginate()


来源:https://stackoverflow.com/questions/38903701/cakephp-3-x-how-to-pass-pagination-configuration-directly-instead-of-using-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!