问题
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