views:

113

answers:

0

My Database is formed like this:

Recipient belongs to a Distribution, Admin HABTM Distribution

Now, when I want to find all the Distributions for which I am an Admin I do as follows:

$userid = $this->Session->read('Auth.User.id');
$meineListen = $this->find('all',array(
 'contain' => array(
  'Recipient' => array(
     'id'
   ),
  'Admin' => array(
     'conditions' => array('Admin.id =' => $userid)
  ),
 )
));

//filtering out all the Distributions that have no Admins (i.e. not me)
for ($i = count($meineListen) - 1; $i >= 0; $i--) {
 if (count($meineListen[$i]['Admin']) == 0) {
  unset($meineListen[$i]);
 }
}

return $meineListen;

Works fine so far, even so it's not overly elegant. Now I also want to have a list of all the recipients, and because those can be rather many, I wanted to paginate the result so the admin is not overwhelmed with data.

According to the Cookbook I can do something like this:

class RecipesController extends AppController {

    var $paginate = array(
        'limit' => 25,
        'contain' => array('Article')
    );
}

But when I try to do more complex containing (like so)...

$userID = $this->Session->read('Auth.Admin.id');
$this->paginate = array(
 'limit' => 10,
 'order' => array(
  'distribution_id' => 'asc',
   'Recipient.title' => 'asc'
 ),
 'contain' => array(
  'Distribution' => array(
   'Admin' => array(
    'conditions' => array('Admin.id' => $userID)
   )
  )
 )
);
$this->set('recipientslist',$this->paginate('Recipient'));

...all I get is a flat hierarchy with Recipients and Distributions, but no show for the Admins whatsoever. And even if I got a result I wouldn't be sure how to get all the entries out that do not belong to me without upsetting the pagination.

So, long story short: how do I get only the recipients in distributions for which I am admin and then paginate the whole lot? Surely there must be a way beyond "overwrite the paginating with a custom pagination function"?!?