CakePHP paginator sorting problem solved!

I’ll keep this brief, because I need to get back to writing code, but I wanted to share the solution I found to a CakePHP problem that has been nagging me for a while and for which I had never found a simple resolution.

I’m using the $paginator->sort() method to create links in the column headers for tables of paginated search results in my CMS. The method works great, except for one small problem: I could never get it to reverse the order. Intuitively, with links like these, you should be able to click them once to sort in ascending order, and then click again to reverse into descending order. But the reverse was never working for me.

Some research showed that you can pass in all sorts of options to the method, but I wanted to avoid having to make a change like that to about a dozen views; plus, it just didn’t seem right — the method is supposed to do the reverse by default.

At last I have discovered the source of my trouble: you need to explicitly name the model in the parameter that defines the sort field. I’ve been getting more diligent about always naming my models explicitly, but back when I set up these paginated tables (which was about the first thing I did in writing the application), I wasn’t doing it yet. Adding in the model, the reverse order on a second click works perfectly. Here’s a before-and-after example to illustrate the problem and its resolution.

Before:

<?php echo $paginator->sort('Title', 'title'); ?>

After:

<?php echo $paginator->sort('Title', 'Article.title'); ?>

Progress…

I realized what a daunting task it was going to be to use the built-in WordPress functionality to manually copy over all of the articles from my old site database, so I dug under the hood and used the power of SQL to just more-or-less dump everything from one database into the other. So now all (well, most) of the stuff is moved over, and I can begin to disassemble the old site (currently lingering in the link set as “Room 34 Cold Storage”).

If you care to look back at the old stuff (and why wouldn’t you?), you may notice some oddities… that’s mainly due to inconsistencies between my idiosyncratic HTML and the fancy formatting/clean-up WordPress does. I’ll gradually fix that stuff… maybe. (Right after I get around to organizing the basement.)

I’m also tinkering more with the themes. I’ve downloaded several interesting themes that I plan to try out for a while, to get a feel for both their visual appeal as well as the ways they handle certain technical features of WordPress. Once I find one I like, I’ll use it as a basis to build my own, one that incorporates my photography as well. (Will the excitement never end?!)

Oh, I almost forgot to mention… some of the old stuff didn’t have dates, so I hastily and more-than-somewhat arbitrarily assigned them all to January 1, 2004.