Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OrderBy filters out row with empty field in EntityFieldQuery

For example, I have a EFQ request like:

$query  ->entityCondition('entity_type', 'node')
        ->entityCondition('bundle', 'contenttype')    
        ->propertyCondition('status', 1)
        ->propertyCondition('language', $language->language, '=')
        ->fieldOrderBy('field_date', 'value', 'DESC')
        ->fieldOrderBy('field_code', 'value', 'DESC')

The field_code is nullable. When I order by this field, it will exclude all rows which got a null value. How to avoid this behavior and let them stay in the result?

like image 744
Mr_DeLeTeD Avatar asked Oct 21 '22 19:10

Mr_DeLeTeD


1 Answers

I ended up having to solve a problem similar to this recently by using a db_select() and just passing it a db_or() for isNull and isNotNull. I don't think an EFQ is flexible enough to do what you need. Something along the lines of this should work:

$query = db_select('node', 'n')
->fields('n')
->condition('n.type', 'contenttype')
->condition('n.status', 1)
->leftJoin('field_data_field_code', 'c', 'n.nid = c.entity_id')
->fields('c');

$db_or = db_or();
$db_or->isNull('c.field_code_value');
$db_or->isNotNull('c.field_code_value');

$query->condition($db_or);

$query->orderBy('c.field_code_value', 'DESC');

$results = $query->execute()->fetchAllAssoc('nid');

if ($results) {
    $nodes = node_load_multiple(array_keys($results));
    return $nodes;
}

One question though - why are you are trying to order by 2 different fields in your example?

like image 188
Nate Avatar answered Oct 23 '22 23:10

Nate