0
votes

I am using contain behavior and i want to be able to sort the data as always but I am having troubles.

It might be because the columns i want to sort are not columns on the paginate table but on the contain tables.

Subscriptions table only contains the "user_id" and the "post_id".

At the controller i have:

$this->paginate = array(
    'conditions' => array('Subscription.user_id' => $this->Session->read('Auth.User.id')),
    'paramType' => 'querystring',
    'limit' => 20,
    'contain'=> array('User', 'Post' => array('Priority', 'Status')) 
 );

$this->set('subscriptions', $this->paginate('Subscription'));

And in my view, this:

<!-- example of not working sort -->
<th class="td_subject"><?php echo $this->Paginator->sort('subject'); ?></th>

The data of subject, for example, is contained on the array $subscription with the normal format:

$subscription['Post']['subject']

And i print it this way:

foreach ($subscriptions as $subscription): 
    echo ...
    echo '<td>'.h($subscription['Post']['subject'].'</td>';
    echo ...
endforeach;

How can i use pagination in this case? Thanks.

1

1 Answers

2
votes

Don't use contain in this case, manually join the tables:

$this->paginate = array(
    'conditions' => array('Subscription.user_id' => $this->Session->read('Auth.User.id')),
    'paramType' => 'querystring',
    'limit' => 20,
    'joins'=>array(
        array(
            'table'=>'users',
            'alias'=>'User',
            'conditions'=>array(
                'Subscription.user_id = User.id
            )
        ),
        array(
            'table'=>'posts',
            ...
        ),
        array(
             'table'=>'priorities',
             ...
        ),
        array(
             'table'=>'status',
             ...
        )
    )
 );

Your view:

<?php echo $this->Paginator->sort('Post.subject', 'Subject'); ?>