Drupal 8 SQL statement, avoiding addOrderBy adding some fields in SELECT

1k views Asked by At

I've got the following line in a custom sort plugin for Views and Drupal 8:

$formula = '(cas.tier IS NULL)';
$this->query->addOrderBy(NULL, $formula, "ASC", "some_alias");

All I want is for Drupal to add this to the existing query:

ORDER BY (cas.tier IS NULL) ASC

But instead here is what it does:

SELECT DISTINCT cas.tier AS some_alias
...
ORDER BY some_alias ASC

So, it adds a field selection in the SELECT statement. While it works for the sorting, it introduces all sort of other issues in what I'm trying to do.

Any idea how to get it to simply add the "ORDER" statement without messing with "SELECT"?

1

There are 1 answers

0
Hubert On

Well, it turns out Drupal can't add a formula in an ORDER BY statement without adding the field as well (for security reasons: https://www.drupal.org/node/829464)

If the ordering doesn't involve a formula, then $this->query->orderby will work without adding a field in the SELECT statement, but with a formula, no no.