If you want to paginate based on the primary model (in this case User, ignoring Like for the pagination), you have two possible options:
1) Add two parameters to the Like::getMostLiked() function to control "pagination" on the array returned, i.e.:
class Like extends AppModel {
...
function getMostLiked($limit = null, $page = 1) {
// if $limit is null, then return all records
...
}
}
class LikesController extends AppController {
...
var $paginate = array(
'limit' => 20,
...
);
function someAction() {
// Load PaginatorHelper manually, since we're not using $this->paginate() ...
$this->helpers[] = 'Paginator';
$liked = $this->Like->getMostLiked($this->limit, $this->params['named']['page']);
$this->loadModel('User');
...
);
2) "Process" only a subset of the array returned by Like::getMostLiked():
$liked = $this->Like->getMostLiked(); // <-- returns a pre-ordered list the way I want
$this->loadModel('User');
$like_list = array();
// Define the start and end of the "page"
if (isset($this->params['named']['page']) {
$start = $this->limit * ($this->params['named']['page'] - 1);
$end = $this->limit * ($this->params['named']['page']);
} else {
$start = 0;
$end = $this->limit;
}
for ($i = $start; $i < $end; $i++) {
$list = $this->User->find('first', array(
'contain' => array('Like'),
'conditions' => array('User.id' => $user )));
array_push($like_list, $list);
}
echo debug($like_list); die; // <-- returns the list of results in exactly the order I want
Now if you want to paginate based on User AND the associated Like, it gets more complicated if User hasMany Like or User hasAndBelongsToMany Like or User hasAndBelongsToMany <someModel> with Like (i.e. Like is the model for the join table between User and <someModel>. That's because some User records may have more than one Like record, which will increase the number of rows displayed. If that's the case, then knowing the association arrays for the User and Like models would be helpful, and possibly the code for getMostLiked().
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
No comments:
Post a Comment