2
votes

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( 'Publication.id = 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.

3
Please post your $conditions array.deceze

3 Answers

2
votes

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');
    $this->set(compact('data'));
}

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

1
votes

I used this idea to create a custom selection of items per page.
My default pagination:

var $paginate = array( 'order' => array('User.id' => 'asc') );

I added this to my controller to change the limit to 25 (also added 1 for 5 and 10 just to see how it would go and changed the limit value accordingly):

function list25() {
//fetches with different limit than default

    $this->paginate = array(
        'limit' => 25,
        'order' => array(
            'User.id' => 'asc'
        )
    );
    $data = $this->paginate('User');
    $this->set('users', $data); 
}

Then, in my main view (index.ctp) I added this at the bottom of the data listing:

<span style="float:right;">
<?php echo "&nbsp;&nbsp;<span style='vertical-align:top;font-size:12px;'>Show:</span>";
      echo $html->link(' 5', array('controller'=>'users', 'action' => 'list5'));
      echo $html->link(' 10', array('controller'=>'users', 'action' => 'list10'));
      echo $html->link(' 25', array('controller'=>'users', 'action' => 'list25'));
    ?>
</span>
<span style="float:right;margin-right:10px;"><?php  echo $paginator->numbers(); ?></span>   

...which shows the following text at the bottom of the display: Show: 5 10 25
The numbers, when clicked, are linked to the controller action and thus resets the limit (and refreshes the page with the new limit set).

Perhaps there is a better way to achieve the same functionality as I had to create a view for each option: list5.ctp, list10.ctp and list25.ctp. Anyway, it provides the user a chance to choose how much data they want to see on a page. Comments?

....noob cakePHP user...

1
votes

Try this:
echo $this->Paginator->link('5',array('limit' => '5')) echo $this->Paginator->link('10',array('limit' => '10')); echo $this->Paginator->link('10',array('limit' => '25'));