Cakephp – how to change pagination condition


on several places in same controller i have code like:

$publications = $this->paginate('Publication', $conditions);

…where $conditions is array of conditions, which fields to select and how (using LIKE in mysql).

every one of them have different "Order by" statement.

how can i achieve that, and define different "order by" part of sql?

UPDATE (2011-03-11): this is part how i defined pagination:

$this->paginate = array(
        'limit' => 1, 
        'fields' => array('`Publication`.*, PublicationNumeration.*, Collection.*, Publisher.*, Publication.title as PublicationTitle'), 
        'joins' => array( array('table' => 'publication_numerations', 'alias' => 'PublicationNumeration', 'type' => 'LEFT', 'conditions' => array( ' = PublicationNumeration.publication_id' ) ) ) 
        , 'order' => array('PublicationTitle desc')

            $publications = $this->paginate('Publication', $conditions);
            $this->set(compact('publications', 'urlArgs'));

part with 'limit' works, but 'order' does not works, and does not show in debug mode at all.

Best Solution

You can override the pagination function for each time that you implement it in each different action. For example:

function list_recipes() {
    $this->paginate = array(
        'limit' => 10,
        'order' => array(
            'Recipe.title' => 'asc'
    $data = $this->paginate('Recipe');

In your case, you'd probably want to reuse your $conditions array but alter the value of 'order' for each different implementation.

Further Reference: Pagination doc

Related Question