On Apr 24, 8:17 am, Michele Ferri <[email protected]> wrote:
> Hi.
>
> I have the following models: User, Membership, Group.
> The Membership model is like a HABTM table, but contains some more
> info, like the role of the user in the group.
>
> I want to have an index page that lists all the groups of the logged
> user.
>
> Controller code
> class GroupsController extends AppController
> {
>          ...
>         function my_index()
>         {
>                 $user_id = $this->Session->read('Auth.User.user_id');
>                 $this->paginate = array(
>                         'conditions' => array('Membership.user_id' => 
> $user_id),
>                         'limit' => 25
>                 );
>                 $this->set('groups', $this->paginate('Membership'));
>         }
>
> }
>
> View code
> <table cellpadding="0" cellspacing="0">
> <tr>
>         <th><?php echo $paginator->sort('name');?></th>
>         <th class="actions"><?php __('Actions');?></th>
> </tr>
> <?php foreach ($groups as $group): ?>
>         <tr>
>                 <td>
>                         <?php echo $group['Group']['name']; ?>
>                 </td>
>                 <td class="actions" style="width: 400px;">
>                         ...
>                 </td>
>         </tr>
> <?php endforeach; ?>
> </table>
>
> The records are displayed fine. The problem is, when I click on the
> table header to sort the results, nothing happens. It is always sorted
> ASC. Also, there is no ORDER BY clause in the select query, in the sql
> debug table.

There's no 'order' key in your $paginate array. Declare $paginate as a
class variable (up at the top, outside of your methods). Maybe you did
do that but, in your example, you've re-declared the variable, rather
than make adjustments to it.

var $paginate = array(
        'Membership' => array(
                'fields' => ...
                'order' => ...
                'limit' => ...
        )
);

function my_index()
{
        // you can use AuthComponent to get User id
        $this->paginate['Membership']['conditions'] = array(
                'Membership.user_id' => $this->Auth->user('id')
        );

        $this->paginate['Membership']['limit'] = 25;

        $this->set('groups', $this->paginate('Membership'));
}


However, you should first ensure that your class-wide $paginate works
correctly before adding the other conditions.

Also, I wonder why you're using pagination here at all. If the results
you're looking for are all the Groups for a particular User do you
really need to paginate them? Are there going to be that many?

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to