logo

Finally – Multiple Model Pagination with Multiple Widgets

logo

Don’t get me wrong – I love CakePHP – im not an expert in it by any means but can quickly knock up a site and it definitely saves me time when coding stuff – no doubt.

One of the main problems I have with it is its pagination classes, true, they work fine if you are just looking to paginate a single model on a page. Whatabout paginating multiple models? For example – say you have a “User Profile” page – on this page you would expect to see things like “Gallery”, “Friends”, “Messages” etc etc – that alone is 3 models.

And say you needed to paginate each of these models to say show only 5 at a time and allow them to be sorted by title, date, etc?

Currently its not very easy with the current set of tools available in cakePHP. Luckily CakePHP has a fantastic community of skilled proggrammers (Certainly more skiilled than I am!).

One such programmer is a guy called Andrew who has released a helper and component out to the community which allows for such functionality.

You can find the code here (http://github.com/angel333/listing/tree/master).

Basically you instantiate the Helper and component in the controller:

class PostsController extends AppController
   {
       public $components = array ('Listing.Listing');
       public $helpers = array ('Listing.Listing');
       ...

Then after setting up some routing you set your data array using something similar to this:

$this->set('data', $this->Listing->create($this->Post, array (
       'default' => array (
           'order' => 'Post.created DESC',
           'limit' => 10,
       ),
       'user' => array (
           'order' => array (
               'Post.title',
               'Post.name',
               'Post.modified',
           ),
           'limit' => array (10, 25, 50, 100),
           'search' => array (
               'Post.title',
               'Post.content',
           ),
       ),
   )));

then call the ever so handy scaffold function to see what code you need to put in.

Everything is on his readme in detail – and from my experience he is very helpful if you contact him!

Good work Andrew :)

Share with your friends and help out this site:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogosphere News
  • E-mail this story to a friend!
  • Identi.ca
  • LinkedIn
  • Live
  • MisterWong
  • MySpace
  • Ping.fm
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Twitter
  • Yahoo! Bookmarks
  • Yahoo! Buzz
logo
logo
Powered by Nu Order Webs