简体   繁体   中英

Symfony 2 / Oracle Querybuilder result is case sensitive

I have a strange behavior with a query result in Symfony 2. I have data like this in an Oracle Database, in this exact order :

  1. BU 4 2015
  2. bU 2
  3. BU C
  4. BU S
  5. BU 4 2014

notice, the lower case in the second data

I Just want to display this list by alphabetical order, without any case sensitive support. The wanted result :

- BU C
- bU 2
- BU 4 2014
- BU 4 2015
- BU S

Now, here is the Querybuilder, made with Symfony 2.5.12 :

 $queryBuilder = $this->createQueryBuilder('e');
 $queryBuilder->orderBy('e.name');

But the query, show my list in this order :

- BU C
- BU 4 2014
- BU 4 2015
- BU S
- bU 2
  • When i play the query displayed in symfony 2 profiler in my Oracle database client, i get the wanted result.

  • When i dump the query builder result, I get the wrong order list.

I manage to solve my problem by this

$queryBuilder = $this->createQueryBuilder('e');
$queryBuilder
    ->select(e)
    ->addSelect('UPPER(e.name) as HIDDEN name')
    ->orderBy('name');

So, I just want to know why I get this strange behavior with the querybuilder ?

Thanks.

First of all, your "wanted result" is not actually in alphabetical order. A case-insensitive sort would yield the following order:

bU 2
BU 4 2014
BU 4 2015
BU C
BU S

Second of all, the way to do a case-insensitive sort with Oracle SQL is to use UPPER() or LOWER() on the column(s) you want to sort by, like so:

SELECT *
FROM e
ORDER BY UPPER(e.name);

Finally, the way to do a case-insensitive sort with Doctrine QueryBuilder is exactly how you have done it: by adding UPPER(e.name) as HIDDEN name and then sorting by that field.

There is no strange behavior by QueryBuilder in this example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM