4
votes

When trying to sort data using the CakePHP Paginator, it is returning case-sensitive sort results. I have tried adding UPPER around the field I specify in the order query, but that doesn't seem to be working. I'm using CakePHP 2.0.3 with a PostgreSQL database. My $paginate variable and the code to call it are:

var $paginate = array(
    'limit' => 25,
    'order' => array(
        'User.first_name' => 'asc'
    )
);
..

(In my view function)
    $users = $this->paginate('User');

How can I force CakePHP to return a case-insensitive sort result?

1
why is the data case sensitive in the first place? shouldnt utf8_unicode_ci (CI stands for case insensitive) solve the issue at its source instead of trying to fix the symptoms? - mark

1 Answers

3
votes

It is a shame that you can't use something like this:

 'UPPER(User.first_name)' => 'ASC'

Which would be fairly basic.

I had this issue in a project, whereby I replaced the 'order' array with something like this:

'order' = "UPPER(first_name) ASC"

which successfully gave me the correct output.

Unfortunately, however, clicking the column header fields to sort my paginated table then replicated the issue, whereby my names were once again case-sensitive.

The solution that I came up with was to modify the PaginatorComponent of cakephp itself, so that my sort order would always be correct.

In the PaginatorComponent (/lib/Cake/Controller/Component/PaginatorComponent.php) I modified the validateSort function, replacing the following line:

$options['order'] = $order;

With the four lines below:

foreach($order as $order_field=>$order_direction){
    $neworder['UPPER('.$order_field.')'] = $order_direction;
}
$options['order'] = $neworder;

It's not elegant, and is essentially just forcing the 'UPPER()' around the field name.

This worked in my particular situation whereby I need to always have case-insensitive queries.