Can I paginate a custom query without overriding the default pagination?

末鹿安然 提交于 2019-12-01 12:38:16

Actually if you CAN do it with find you CAN do it with paginate. You can take a look here

But to be more especific you can add the conditions/limit/fields/contain/order etc that you use in find to the paginate function.

I haven't use group in the paginate but it SHOULD work :D

In your case you will have something like this:

$this->paginate = array(
   'fields' => array(
        'Product.category_id',
        'COUNT(Product.hotel_id) as total'
    ),
   'group' => array(
        'Product.category_id HAVING COUNT(Product.hotel_id) > 1')
    )
);

$data = $this->paginate('Product');

Hope it works, post a comment of your result, if it doesn't work you will have to override it, because it is not accepting the group condition... though I think it will work since pagination is a find in the end.

EDIT:

You may try to do something like this:

Override the paginate() and paginateCount() but with a tweak, sneak a condition so you can tell if its a pagination with having or not. Something like this:

function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()){
   //if no having conditions set return the parent paginate 
   if (empty($conditions['having'])
       return parent::paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra)
   //if having conditions set return your override

//override code here

}

Then you do something similar in paginateCount(), that way you have a selective paginate. remember to do unset $conditions['having'] once it is not needed or remember to put it somewhere that doesn't affect your find ;)

Tom Wright

Actually getting this to work was more of a headache than you might reasonably expect.

Although I basically followed the advice of api55 (thanks!) I also jumped a load of other hurdles:

  • It's not possible to do parent::paginateCount(). I overcame this by overriding it with a different (and apparently better) version in app_model.php.

  • Because paginateCount() is just a wrapper for find('count'), it doesn't accept a fields parameter. This is tricky for me as I rely on this to squeeze in my derived column (score of a full-text search). I got over this by passing the value of fields twice to paginate - once as fields and once as "sneaky". Cake puts any parameters it doesn't recognize into the extra array.

  • Tying this together, I had an override of paginateCount() in my model that looks to see whether extra has an key called "sneaky". If it does, it does a find('all') and uses the contents of sneaky to populate fields.

It's days like today that I have to step back and remember all the good points about using a framework.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!