1

I have a Views filter handler, which does

$this->query->add_where(
  $this->options['group'],
  $this->real_field,
  vms_get_current_mlid(),
  $this->operator = ($this->operator == '=') ? 'IN' : 'NOT IN'
);

Which initially looks fine, but fails with "NOT IN", when the field value is NULL, as NOT IN (100) does not return TRUE for a NULL value, which is what I would like.

When the operator is 'NOT IN', I need to add a "OR IS NULL" operator as well, but still keep it within the same group.

If the filters are

Node is "page" AND NID_REFERENCE NOT IN (100, 101, 102)

I would like that to become

Node is "page" AND (NID_REFERENCE NOT IN (100, 101, 102) OR NID_REFERENCE IS NOT NULL)

How do I do that?

2 Answers 2

1

The answer turns out to be that add_where() also accepts a DatabaseCondition argument, meaning I could do

$this->query->add_where(
  $group,
  db_or()
    ->condition($table, $vms_target, 'NOT IN')
    ->condition($table, $vms_target, 'IS NULL')
)
1
  • Doh! Glad you got it working!
    – Andy
    Commented Aug 8, 2012 at 12:24
0

I couldn't quite wrap my head around those queries in your last comment, but instead I tried exploring how Views does between operations. Check out db_or() which is used by views_handler_filter_numeric::op_between() - I think that will do it.

4
  • I tested this, and the problem is that the filter is moved to a new group, which means the View will no longer respect the filter group settings. Somehow the existing group needs a sub group, for this to work.
    – Letharion
    Commented Aug 8, 2012 at 7:56
  • @Letharion I'm not sure what you mean by the filter group settings? Isn't the only group setting the operator (ie AND or OR)? I think I'm missing something :)
    – Andy
    Commented Aug 8, 2012 at 8:35
  • My bad. If the View has, set in the UI, two groups of filters, like (node.type = 'page' AND nid_ref NOT IN (100, 101)) AND (nid_ref IN (102)), your suggestion turns this into (node.type = 'page') AND (nid_ref IN (102)) AND/OR (nid_ref NOT IN (100, 101)) as opposed to (node.type = 'page' AND (nid_ref NOT IN (100, 101) OR nid_ref IS NOT NULL)) AND (nid_ref IN (102)) because you add a $new_group, instead of manipulating the existing one.
    – Letharion
    Commented Aug 8, 2012 at 9:15
  • Hehe, yeah, the queries got kinda long :P Suffice it to say that the parenthesis moved around in a way that broke the query. :)
    – Letharion
    Commented Aug 8, 2012 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.