4

CakePHP Paginator を使用してデータをソートしようとすると、大文字と小文字を区別するソート結果が返されます。注文クエリで指定したフィールドの前後に UPPER を追加しようとしましたが、うまくいかないようです。PostgreSQL データベースで CakePHP 2.0.3 を使用しています。私の $paginate 変数とそれを呼び出すコードは次のとおりです。

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

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

CakePHP に大文字と小文字を区別しないソート結果を返すように強制するにはどうすればよいですか?

4

1 に答える 1

3

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.

于 2013-01-04T02:07:03.070 に答える