DDC-775: Ability to use IFNULL() function in ORDER BY clause #954

Closed
opened 2026-01-22 12:57:08 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Aug 29, 2010).

Jira issue originally created by user darkangel:

    $qb = $em->createQueryBuilder(); /** @var $qb Doctrine\ORM\QueryBuilder **/
    $roles = $qb->from('App*Model*Role', 'r')
                ->select('r')
                ->orderBy('IFNULL(r.rootId, r.id), r.lft')
                ->getQuery()
                ->getResult();

[Syntax Error] line 0, col 46: Error: Expected end of string, got '('

Originally created by @doctrinebot on GitHub (Aug 29, 2010). Jira issue originally created by user darkangel: ``` $qb = $em->createQueryBuilder(); /** @var $qb Doctrine\ORM\QueryBuilder **/ $roles = $qb->from('App*Model*Role', 'r') ->select('r') ->orderBy('IFNULL(r.rootId, r.id), r.lft') ->getQuery() ->getResult(); ``` [Syntax Error] line 0, col 46: Error: Expected end of string, got '('
admin added the Improvement label 2026-01-22 12:57:09 +01:00
admin closed this issue 2026-01-22 12:57:10 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 20, 2012):

Comment created by riki4321:

I have the same error while using FIELD mysql function.

I added beberlei/DoctrineExtensions to my Symfony2 project to enable the FIELD function, but I'm able to use it in SELECT clause only. The problem is that in that case Doctrine returns bad result for me. Here is the example:

           $queryBuilder
                 ->addSelect('FIELD(u.id, 1, 2) as relevanceField')
                 ->orderBy('relevanceField')
            ;

The result of

foreach($queryBuilder->getQuery()->getResult() as $r) { ... } 

is:

Array(Object, relevanceField)
Array(Object, relevanceField)
...

Without that addSelect() function call I have correct result:

Object
Object 
...

So I think orderByItem() function in Parser.php class should be extended so that MySQL function could be used there.

@doctrinebot commented on GitHub (Feb 20, 2012): Comment created by riki4321: I have the same error while using FIELD mysql function. I added beberlei/DoctrineExtensions to my Symfony2 project to enable the FIELD function, but I'm able to use it in SELECT clause only. The problem is that in that case Doctrine returns bad result for me. Here is the example: ``` $queryBuilder ->addSelect('FIELD(u.id, 1, 2) as relevanceField') ->orderBy('relevanceField') ; ``` The result of ``` foreach($queryBuilder->getQuery()->getResult() as $r) { ... } ``` is: ``` Array(Object, relevanceField) Array(Object, relevanceField) ... ``` Without that addSelect() function call I have correct result: ``` Object Object ... ``` So I think orderByItem() function in Parser.php class should be extended so that MySQL function could be used there.
Author
Owner

@doctrinebot commented on GitHub (Feb 20, 2012):

Comment created by @FabioBatSilva:

Hi Nikita,

I have a pull request under discussion about this ticket :
https://github.com/doctrine/doctrine2/pull/288

please follow, and fell free to comment. :)

@doctrinebot commented on GitHub (Feb 20, 2012): Comment created by @FabioBatSilva: Hi Nikita, I have a pull request under discussion about this ticket : https://github.com/doctrine/doctrine2/pull/288 please follow, and fell free to comment. :)
Author
Owner

@doctrinebot commented on GitHub (Mar 25, 2012):

Comment created by @FabioBatSilva:

i'm +1 for this feature.
But is not cross-platform then we can't support..

More details : https://github.com/doctrine/doctrine2/pull/288

@doctrinebot commented on GitHub (Mar 25, 2012): Comment created by @FabioBatSilva: i'm +1 for this feature. But is not cross-platform then we can't support.. More details : https://github.com/doctrine/doctrine2/pull/288
Author
Owner

@doctrinebot commented on GitHub (Mar 25, 2012):

Issue was closed with resolution "Won't Fix"

@doctrinebot commented on GitHub (Mar 25, 2012): Issue was closed with resolution "Won't Fix"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#954