So I've got a query object with the following data in it (outputted into watchdog) - this is from hook_views_post_execute() -

[#conjunction] => OR
    [0] => Array (
    [field] => SelectQuery Object
    [fields:protected] => Array (
        [nid] => Array (
            [field] => nid
            [table] => na
            [alias] => nid
    [value] => EXISTS
    [operator] => =
    [1] => Array
                                                                    [field] => node.nid
                                                                    [value] => IS NULL
                                                                    [operator] => =

Now as far as I can tell, according to this "OR" condition in db_select() this is how this is supposed to look for an OR condition.

However when I look at the string query for this, it looks like this:

WHERE (( (na.gid = :db_condition_placeholder_2) AND (na.realm = :db_condition_placeholder_3) )OR( (na.gid = :db_condition_placeholder_4) AND (na.realm = :db_condition_placeholder_5) )OR( (na.gid = :db_condition_placeholder_6) AND (na.realm = :db_condition_placeholder_7) )OR( (na.gid = :db_condition_placeholder_8) AND (na.realm = :db_condition_placeholder_9) ))AND (na.grant_view >= :db_condition_placeholder_10) AND (node.nid = na.nid) )OR (node.nid = :db_condition_placeholder_11) 

:db_condition_placeholder_11 equaling IS NULL.

Now the actual query I want is something along the lines of this:

AND ( EXISTS (SELECT na.nid AS nid FROM node_access na WHERE (( (na.gid = 0) 
AND (na.realm = 'all') )OR( (na.gid = 117) 
AND (na.realm = 'content_access_author') )OR( (na.gid = 2) 
AND (na.realm = 'content_access_rid') )OR( (na.gid = 10) 
AND (na.realm = 'content_access_rid') ))
AND (na.grant_view >= 1) 
AND (node_field_data_field_consultant_contact_er.nid = na.nid) ) OR node.nid <typically an entity reference to a node here> IS NULL ) 

I am generating it like this:

$query->condition(db_or()->condition($inside_subquery, "EXISTS")->isNull($values[0]));

What exactly am I doing wrong? The $inside_subquery variable has all of the data that should be in the EXISTS in the above query.

Why does the string value differ from the query object?

  • Interesting question. I've never used the exists operator, and it does look like the condition method should take a sub-query. I noticed in the drupal 7 simpletests for DBTNG that exists is using an exists method and that does take a sub-query. I wonder if you'll have any luck with that instead? Actually it takes a sub-query object, not the actual string. – mradcliffe Jun 9 '16 at 2:18
  • Actually, despite the query on the DatabaseConnection object being incorrect, on cache clear it appears like the query is working. So I guess I can close this out? – Jack Ryan Jun 9 '16 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.