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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With