By default, MySQL will still sort the NULL value; it will simply put it at the beginning of the result set if it was sorted by ASC , and at the end if it was sorted by DESC . Here you want to sort the ASC , but you want the NULL values ββto be at the bottom.
Unfortunately, as powerful as Doctrine, it will not offer much support here, as function support is limited, and most of it is limited to SELECT , WHERE and HAVING . You really would not have a problem at all if the following were true in QueryBuilder:
select() accepted ISNULL()orderBy() or addOrderBy() supported by ISNULL()- the class supported the concept of
UNION (in this case, you could run two queries: one where codeIata was NULL , and one where it was not, and you can sort each one independently)
So, you can go with the user-defined functions that were already mentioned in ArtWorkAD, or you could replicate this last point with two different Doctrine queries:
$airlinesWithCode = $er->createQueryBuilder("airline") ->where("airline.iataCode IS NULL") ->getQuery() ->getResult(); $airlinesWithoutCode = $er->createQueryBuilder("airline") ->where("airline.iataCode IS NOT NULL") ->getQuery() ->getResult();
Then you can combine them into one array or independently process them in your templates.
Another idea is to return DQL all to one dataset and allow PHP to do a heavy lift. Sort of:
$airlines = $er->findAll(); $sortedAirlines = array(); // Add non-NULL values to the end if the sorted array foreach ($airlines as $airline) if ($airline->getCodeIata()) $sortedAirlines[] = $airline; // Add NULL values to the end of the sorted array foreach ($airlines as $airline) if (!$airline->getCodeIata()) $sortedAirlines[] = $airline;
The disadvantage of two of them is that you cannot do LIMIT in MySQL, so it can only work for relatively small datasets.
Anyway, hope this helps you!
source share