Can i override CakePHP 2 paginate limit?

I do create a new array $dataNewResult and push data into it inside the controller data results foreach loop.

I know it should be done into the model but i could not modify the query into the model.

How can i override the limit property from the paginate object to match the count ?

$this->paginate[‘limit’] = $dataNewResult;

I am getting this error message:
Indirect modification of overloaded property MyobjectController::$paginate has no effect

I am trying this code :
if(base64_decode($this->passedArgs[‘Myobject.restriction’]) == ‘0’)
{
$this->set(‘data’, $dataNewResult);
$this->paginate[‘limit’] = count($dataNewResult);
} else {
$this->set(‘data’, $data);
}

Once in the controller action, $this->paginate becomes an object, and you can’t override is directly.

In the action, you need to go via the actual $this->Paginator->paginate() call, or $this->Paginator->settings, as per the last paragraphs of http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#query-setup.

Actually, here’s your error message described in this section: http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#pagination-with-get-parameters

1 Like

I am trying this code:

Into the controller class i do declare into the components array and after i do set the propety:
public $components = array(‘Paginator’);

$this->Paginator->settings[‘limit’] = count($ $dataNewResult);

This did not set the number of the count to the limit setting of the Paginator class.

Where i am doing wrong ?

As an alternative would be possible override into the view the paginator message: Showing 1 to 30 from 57 results. If i found just 3 would be Showing 1 to 3 from 3 results.

After setting the limit, are you calling $this->Paginator->paginate() again, so that it can run with the newly set limit?

Also, you might want to call make the query a $this->find('count', ...) rather than $this->find('all', ...) to reduce the data that is returned by the database server. Then in pseudo code, your controller might look like this:

if ( /* no filter */) {
   $count = $this->find('count', /* unfiltered conditions */);
   $this->Paginator->settings['limit'] = $count;
   $data = $this->Paginator->paginate(/*unfiltered conditions */);
} else {
   $data = $this->Paginator->paginate(/* filtered conditions */);
}
$this->set('data', $data);

With this code, if there is no filter/restrictions, we determine that the limit of the pagination is the same as the predicted results, set the limit accordingly, and let the paginator do it’s thing. If there are restrictions, then Paginate still doesn’t it’s thing, but with different conditions, and the default limit.