| Paginate's "group" attribute causes the page count to be wrong |
|
|
|
| CakePHP - Basics | |
| Written by Stuart Duncan | |
| Friday, 04 December 2009 15:56 | |
|
Shortly after figuring out how to do joins in the paginate property, I ran into my next problem.... grouping the results caused paginate to report the wrong number of pages! It turns out that the only place that reports this, as well as having a fix for it, is CakePHP's own bug tracking!! Once I made the join property work, I realized that my results were showing multiples any time that the joined table had more than one record associated to the main table and as we all know, the fix for this is to group your results. Unfortunately in CakePHP, paginate thinks that grouping results means putting everything onto one page. Well, not exactly but that's the end result. CakePHP's 'find()' function however, does not. It has no issues dealing with groups. So after checking out CakePHP's bug tracker, I found the solution to my problem. In your model file, mine is 'model/Actor.php', add in this code:
Essentially what this does is override the pageinateCount() method that is already deep within CakePHP. So now you are in control instead of CakePHP's core... and what this control allows you to do is transfer the 'count' query to CakePHP's 'find()' method so that it returns a proper number of results, unlike the 'paginate()' method. So you're leaving the actual results to the paginate as it should be, but returning the 'count' of the results from the 'find()' method instead so that grouping doesn't cause a problem. It's kind of a hack/fix, but not really as you are still using CakePHP's conventions and it does say right in the manual that you can override these functions... so, in the end, what ever works... works. |




