views:

42

answers:

2

I've managed to over-ride the default methods for a custom query in my model as suggested elsewhere,

function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array())

and

function paginateCount($conditions = null, $recursive = 0, $extra = array())

Unfortunately this approach over-rides all pagination for this model, and affects other pagination elsewhere. I found some code which may help where I could select whether I wanted the custom pagination used based on a variable e.g.

In my model

var $useCustom = false;

function paginateCount($conditions = null, $recursive = 0, $extra = array())
{
if(!$this->useCustom)
    return parent::paginateCount($conditions, $recursive);

// code to handle custom paginate count here
}

I have found that using this method gives me an error,

Fatal error: Call to undefined method AppModel::paginateCount() in....

What am I doing wrong? I assume that I would also need similar code in the paginate function as well? Am I also correct in thinking that I can set this variable in my controller i.e. $this->useCustom = 'true';

A: 

I think you need the public keyword before your function to use the scope resolution operator in this way.

i.e. public function paginateCount(....

D Roddis
Thanks, but this did not work, please see my solution
Dave
+1  A: 

After a bit of delving into the code I found that the methods of paginateCount and paginate do not exist in the Model, or anywhere else for that matter, which is why I could not call them. The solution was copy the code from the main controller, which tests for the existence of the over-ride

For those that would like a similar solution use the following in paginateCount

if(!$this->useCustom)
{
    $parameters = compact('conditions');
    if ($recursive != $this->recursive) {
        $parameters['recursive'] = $recursive;
    }
    $count = $this->find('count', array_merge($parameters, $extra));

    return $count;

} else {

custom method...

}

and in paginate use

if(!$this->useCustom)
{
    $parameters = compact('conditions', 'fields', 'order', 'limit', 'page');
    if ($recursive != $this->recursive) {
        $parameters['recursive'] = $recursive;
    }
    $results = $this->find('all', array_merge($parameters, $extra));
    return $results;

} else {

custom method...

}

Hope this helps someone else.

Dave