Using pagination with CakePHP’s finderQuery SQL

Cakephp 1.2

Update February 2009: this article is now about a year old and deals with an early beta versions of CakePHP 1.2. In the mean time, CakePHP 1.2 has been released with many improvements and great documentation. Please don’t use this patch, use this instead. I’ll leave the article below for reference.

Cakephp lets you define various kinds of associations between models. The principle is really simple, just define arrays like $hasMany in your main model, where associations with other models are defined.

For most purposes this does the trick, most associations are made by convention, but you can override these as well for e.g. database (foreign-) keys.

From there on, you can use all cake built-in functionality to retrieve model data. One such feature is auto pagination, where the page size is set through the value of the limit field in the model’s definition.

If you’re crafting something really complex, you can define your own SQL query in the finderQuery field. This is very nice, but the trade-of is that the other fields are ignored in the query. And if fields limit and offset are ignored, you no longer get auto pagination.

The patch below fixes this for the current development tree of CakePHP 1.2 (revision 6461).
Read the rest of this entry »

Popularity: 82%

Post to Twitter Tweet This Post to Delicious Delicious Post to Reddit Reddit This Post Post to StumbleUpon Stumble This Post

Tags: , , , , , ,
Comments off

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.