I hate all the pagination params in the controller so I did this
To paginate all active users for an example
controller:
$this->paginate['User'] = $this->User->userPagination();
$this->set('users', $this->paginate('User', array('User.id' =>
$this->User->getActiveUsers())));
model:
Build pagination as normal:
function userPagination()
{
$params = array(
'fields' => array(
'User.id',
'User.name',
'User.city'),
'limit' => 20,
'order' => array(
'User.name DESC'),
'contain' => array(
'State' => array(
'fields' => array('State.abrev')),
'Country' => array(
'fields' =>
array('Country.name'))));
return $params;
}
public function getActiveUsers()
//this fetches all Users who have been admin approved
{
$params = array(
'conditions' => array(
'User.status' => '1'),
'fields' => array(
'User.id'),
'contain' => false);
$q = $this->find('all', $params);
//create an array of User.id's to pass to paginate
$active = array();
//return only the id
foreach ($q as $active_user)
{
array_push($active, $active_user['User']['id']);
}
return $active;
}
Has anyone found a cleaner or easier way to do this?
I needed to paginate different sets in the same model and the configuration
was driving me crazy so this way I found I can have different paginations
for the same model with different params.
getActiveUsers()
usersByCountry($country_id) and so on
Dave
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---