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

后端 未结 2 740
北恋
北恋 2021-01-16 15:43

In my CakePHP (1.2) app, I have two actions that both use pagination - index and search.

In a previous question I learnt that, in order to apply a threshold score to

2条回答
  •  伪装坚强ぢ
    2021-01-16 16:31

    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 ;)

提交回复
热议问题