0
votes

i am developing with cakephp (2.4.7) and i have problems with organizing my controllers and models to use pagination.

So far i put the most logic into the models (thin controller, big model). There i returned the results to the controller where i set the variables to display it on the view.

But now i want to use pagination. This break my concept because i can not use pagination inside the models.

Whats the best solution to solve this problem? I do not want to reorganzie my whole structure, because i need pagination in a lot of different actions and models.

For example:

Controller Users, action friends

public function friends($userid = null, $slug = null) {
    $this->layout = 'userprofile';      
    $this->User->id = $userid;
    if (!$this->User->exists()) {
        throw new NotFoundException(__('Invalid User'));
    }

    $this->set('friends', $this->User->getFriendsFrom($userid));
}

User Model, function getFriendsFrom($user_from).. i need this method in different actions.

public function getFriendsFrom($user_from) {

    $idToFind = $user_from;

    $data = $this->FriendFrom->find('all', 
        array(
            'conditions'=>array(
                'OR'=> array(
                    array('user_to'=> $idToFind),
                    array('user_from'=> $idToFind)
                ),
                'AND' => array(
                    'friendship_status' => 1
                )
            ),
            'contain' => array('UserFrom.Picture', 'UserTo.Picture')
        )
    );

    $friendslist = array();
    foreach ($data as $i) {
        if ($i['FriendFrom']['user_from'] == $idToFind){
            $friendslist[] = $i['UserTo'];
        }
        elseif ($i['FriendFrom']['user_to'] == $idToFind){
            $friendslist[] = $i['UserFrom'];
        }
    }

    return $friendslist;
}

Whats the best way to design this concept to use pagination?

Thanks

1

1 Answers

0
votes

in Controller Users use cakephp Paginator

var $helpers = array('Paginator');

Now you call the following method

function index() {
    $result = array(
        'recursive' => -1,
        'conditions' => array(...),
        'contain' => array(...),
        'limit' => '2'
    );
    // you can write the above code in your model
    $this->paginate = $result; 
    $users = $this->paginate('User');

    // Re-arrage $users 

    $this->set(compact('users'));
}

If any problem, let me know.