Re: Doing pagination without paginate()

2008-12-29 Thread gearvOsh

Ive been messing around with it a bit and got this, is this the right
way to do things?

class DashboardController extends AppController {
var $uses = array('Friend');

// Pagination
var $paginate = array(
'Friend' => array(
'limit' => 5,
'order' => array('Friend.requestTime' => 'ASC'),
'recursive' => -1
)
);

function index() {
$this->paginate['Friend'] = array('extra' => array('user_id' =>
$this->Auth->user('id')));
debug($this->paginate('Friend', array('Friend.status' =>
'approved')));
}
}

And the custom paginate query:

function paginate($conditions, $fields, $order, $limit, $page = 1,
$recursive = null, $extra = array()) {
$conditions[] = array('OR' => array(
'Friend.user_id' => $extra['extra']['user_id'],
'Friend.friend_id' => $extra['extra']['user_id']
));

$results = $this->findAll($conditions, $fields, $order, $limit,
$page, $recursive);
$cleanResults = array();

if (!empty($results)) {
foreach ($results as $friend) {
if ($friend['Friend']['friend_id'] != 
$extra['extra']['user_id']) {
$friend_id = $friend['Friend']['friend_id'];
} else {
$friend_id = $friend['Friend']['user_id'];
}

$userObj = $this->User->find('first', array(
'fields' => array('User.id', 'User.username', 
'User.avatar',
'User.handle', 'User.country_id', 'User.signupDate'),
'recursive' => -1,
'conditions' => array('User.id' => $friend_id)
));

if (!empty($userObj)) {
$cleanResults[] = array_merge($friend, 
$userObj);
}
}
}

return $cleanResults;
}

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



Doing pagination without paginate()

2008-12-29 Thread gearvOsh

Im having problems getting pagination working. I have complex custom
queries and I just cant get it working correctly. Ive tried building a
custom paginate() method within the model that does additional
filtering, but then I also feel limited using the $paginate var in the
controller.

Anyone have suggestions here?

This is the method im trying to turn into paging (cant seem to get the
$user_id and $status passed in (its dynamic of course):

function getMyFriends($user_id, $status = 'approved', $limit = 5) {
$friends = $this->find('all', array(
'fields' => array('Friend.*'),
'recursive' => -1,
'conditions' => array(
'Friend.status' => $status,
'OR' => array(
'Friend.user_id' => $user_id,
'Friend.friend_id' => $user_id
)
),
'order' => 'Friend.requestTime ASC',
'limit' => $limit
));

$cleanFriends = array();
if (!empty($friends)) {
foreach ($friends as $friend) {
if ($friend['Friend']['friend_id'] != $user_id) {
$friend_id = $friend['Friend']['friend_id'];
} else {
$friend_id = $friend['Friend']['user_id'];
}

$userObj = $this->User->find('first', array(
'fields' => array('User.id', 'User.username', 
'User.avatar',
'User.handle', 'User.country_id', 'User.signupDate'),
'recursive' => -1,
'conditions' => array('User.id' => $friend_id)
));

if (!empty($userObj)) {
$cleanFriends[] = array_merge($friend, 
$userObj);
}
}
}

return $cleanFriends;
}

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