castarco
castarco

Reputation: 1376

Efficient pagination with CakePHP

I have an "architectural" problem with CakePHP :p .

I have to paginate queries, that seems easy, I use the $paginate array and the paginate method, but I have many restrictions.

In many methods of my controller I must return different fields of the same Model, and in both cases I have to paginate. This fact forces me to mention all the fields in the $paginate array, and this could cause poor performance when I don't need those fields.

How can I set different paginate rules for different methods in a clean way?

(I thought in using different arrays and assign to $paginate the specific array in runtime, but I want to know if there is an "oficial way" to do it)

Upvotes: 0

Views: 265

Answers (2)

lurkit
lurkit

Reputation: 398

I don't know if this is what you are already doing, but I think this is already clean enough:

function foo() {
    $this->paginate['fields'] = array('field_1', 'field_2');
    /* rest of the method */
}
function bar() {
    $this->paginate['fields'] = array('field_3', 'field_4');
    /* rest of the method */
}

If there are fields that you are going to use in all methods you could also do it like this:

var $paginate  = array (
    'fields' => array('always_need_this', 'also_need_this_always',)
);
function foo() {
    array_push($this->paginate['fields'], 'only_in_foo', 'also_only_in_foo');
    /* rest of the method */
}
function bar() {
   array_push($this->paginate['fields'], 'only_in_bar', 'also_only_in_bar');
    /* rest of the method */
}

Upvotes: 0

Anh Pham
Anh Pham

Reputation: 5481

if your table doesn't have a massive number of fields, I think it's ok to let cake query all of them. The performance shouldn't be much different.

You can specify different pagination sets:

var $paginate = array(
   'Recipe' => array (...),
   'Author' => array (...)
);

then $data = $this->paginate('Recipe');

http://book.cakephp.org/view/1232/Controller-Setup

Upvotes: 1

Related Questions