tags:

views:

120

answers:

2

Cake handles pagination of a model with a simple $this->paginate(), but what should I use if I want to paginate a array of values?

The Scenario is like this:

$this->set('sitepages', $this->paginate());

This code in my index() returns an array like

Array
(
    [0] => Array
        (
            [Sitepage] => Array
                (
                    [id] => 13
                    [name] => Home
                    [urlslug] => home
                    [parent_id] => 1
                    [page_title] => Welcome to KIAMS, Pune
                    [order] => 1
                )   
        )
    [1] => Array
        (
            [Sitepage] => Array
                (
                    [id] => 26
                    [name] => About Us
                    [urlslug] => aboutus
                    [parent_id] => 1
                    [page_title] => 
                    [order] => 2
                )
        )
    [2] => Array
        (
            [Sitepage] => Array
                (
                    [id] => 27
                    [name] => Overview of KIAMS
                    [urlslug] => aboutus/overview
                    [parent_id] => 26
                    [page_title] => 
                    [order] => 2
                )
        )

I retrieved the same data using $this->Sitepage->find('all') and then performed some manipulations as required and form a array which is very similar to the above one, but the ordering gets changed. I want to paginate this new array and pass it to the view. I tried

$this->set('sitepages',$this->paginate($newarray))

But the data is not getting paginated. Can some one please help with paginating the $newarray in CakePHP?

A: 

To paginate in CakePHP you need to pass select conditions to the paginate() call.

Other data manipulation should be done in afterFind(), in your model file.

If you don't need these changes to be done in every single retrieval, you might as well consider creating a new model file pointing to the very same table as the current one, and adding an afterFind() method to that new file.

Seb
Creating a model in my opinion would be a overkill.. Surely there must be some simple way to paginate an array of data...
Ashok
Does the PaginatorHelper assist in any way? It looks like you can use it to paginate data from within the view. It may do what you need. http://book.cakephp.org/view/166/Pagination-in-Views
Travis Leleu
@Ashok When you use a framework things that should be easy in others not necessarily are in this one. They give you some great features, but when you need some extra customization you'll probably get into ad-hoc solutions in all possible frameworks, not just CakePHP.
Seb
@Travis, Paginator Helper will help in displaying the paginated data received from the controller... We can't use the helper to paginate data from within the view. All paginations happen in the Controller and then come to view...
Ashok
@Seb, Agreed. I thought there would be some way, but I guess not since the Paginator definitely takes in a Model only. Thanks to everyone for your time..
Ashok
A: 

$this->paginate($newarray) is the wrong way to paginate. The first parameter cannot be an array. It must be a model name. You may want to study pagination setup from the manual. This will order alphabetically:

var $paginate = array(
    'limit' => 25,
    'order' => array(
        'Sitepage.name' => 'asc'
    )
);
$this->set('sitepages', $this->paginate('Sitepage'));
bancer
You didn't understand the question... the problem is not with pagination, but with some tweaked resultset he wants to paginate.
Seb
Seb said it....
Ashok