cake-logo

CakePHP 1.3.3 Broken Pagination Sort Options

Working with CakePHP v1.3.3 and the Pagination helper I discovered that the Direction option for the Sort method sort of goes ignored— or more like over-written with whatever value CakePHP decides to use.   I’m not happy with my hack, but I need this functionality today.  I’m also not too fond of having to hack-up source code when upgrades will be involved because I have to keep porting forward my patches.  So anyhow.  Here’s what I’m trying to do.

In my view, I’d like to have sort options such as, Most Recent, Most Viewed, Longest Video, Shortest Video.  I’ve tried hard-coding the URL for the Paginator to use, this didn’t work well with some of the search options.  I tried to be crafty with $this->here, $this->action and $this->name.   I figured it’d be best to let the Paginator deal with it.  So I dug up the documentation on $this->Paginator->sort();  Set ‘direction’=>’desc’, and the generated link wasn’t in Descending order. :-S   Here’s what I found.

$dir = isset($options['direction']) ? $options['direction'] : 'asc';
unset($options['direction']);

$sortKey = $this->sortKey($options['model']);
$defaultModel = $this->defaultModel();
$isSorted = (
	$sortKey === $key ||
	$sortKey === $defaultModel . '.' . $key ||
	$key === $defaultModel . '.' . $sortKey
);
if ($isSorted) {....}

I changed it to check for the Direction option.

$dir = isset($options['direction']) ? $options['direction'] : 'asc';
//unset($options['direction']);

$isSorted = false;
if (!isset($options['direction']))
{
	$sortKey = $this->sortKey($options['model']);
	$defaultModel = $this->defaultModel();
	$isSorted = (
		$sortKey === $key ||
		$sortKey === $defaultModel . '.' . $key ||
		$key === $defaultModel . '.' . $sortKey
	);
}

if ($isSorted) {....}

Seems to work with all my other pagination throughout the site. I haven’t tested it heavily, but it appears to work.

, , ,

Comments are closed.