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

后端 未结 2 769
北恋
北恋 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:23

    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.

提交回复
热议问题