Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7711909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:29:45+00:00 2026-06-01T01:29:45+00:00

When you sort on column a descending, it will put all records having a

  • 0

When you sort on column a descending, it will put all records having a NULL at the bottom, which makes sense. What I want is the following ordering when I’m sorting on attribute a descending:

a
----
NULL
NULL
5
4
3
2
1

I know Oracle has NULLS_FIRST and in MySQL you could use ORDER BY ISNULL(a), but I’m wondering whether there is an elegant way to deal with this using Doctrine2 functionality only? So not using native queries, etc.


Answer with the help of S0pa: I fixed it using the following new function node for Doctrine2:

<?php
namespace MyBundle\General;

use \Doctrine\ORM\Query\Lexer;

class IsnullFunctionNode extends \Doctrine\ORM\Query\AST\Functions\FunctionNode
{
    private $isnull;

    public function parse(\Doctrine\ORM\Query\Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->isnull = $parser->ArithmeticPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }
    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
        return 'ISNULL('.$this->isnull->dispatch($sqlWalker).')';
    }
}

And connected it in Symfony2 to Doctrine2 like this (app.yml):

doctrine:
  orm:
    entity_managers:
      default:
        ...
        dql:
          numeric_functions:
            isnull: Diagenda\CommonBundle\General\IsnullFunctionNode

I can’t use these functions in my ORDER BY clause, but by doing a SELECT ISNULL(a) AS n_a I can order on the n_a attribute.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:29:46+00:00Added an answer on June 1, 2026 at 1:29 am

    I think the best way is to define your own DQL function, using this tutorial. You will still need to handle different case depending on your DB management system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code like following: switch(sort.Column) { case code: model = (sort.Direction == SortDirection.Ascending)
I have some PHP code which allows me to sort a column into ascending
I want to sort by a column where I want numbers to be ASC,
I want to sort on a certain column only for rows containing a certain
I am trying to query my database and sort my start column in descending
I'm using SlickGrid with DataView and I'm trying to set a default sort column
I'm trying to sort a column of data in a GTK tree view non-alphabetically.
I can't get my GridView to enable a user to sort a column of
I have a custom, multi-column sort attached to my jqGrid instance by means of
I'd like to sort on a column in the result of a stored procedure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.