1

I have the following raw SQL:

 SELECT *
 FROM node_field_data n
 LEFT JOIN book b ON n.nid=b.nid
 WHERE n.type IN ('book')
 AND (b.has_children != 1 OR b.nid IS NULL)

I have a module that I need to add this query to, so I already have a $query object. The problem I'm having is finding the documentation/syntax to do the LEFT JOIN part of my query.

This is what I've added to $query.

 $query->addWhere(0,'node.nid', 'b.nid');
 $query->addWhere(0,$types,'IN');
 $query->addWhere(0,'b.has_children',1,'<>');
 $query->addWhere(1,'b.nid','IS NULL');

How do I add the LEFT JOIN part?

4

The method is actually named leftJoin:

$query->leftJoin('book', 'b', 'n.nid = b.nid');
  • Found that and your answer at the same time. Going to mark it as such. Still getting no results, but better than earlier when I was getting errors. Is there a way to print out the query using logger? – StevenPatz Jul 25 '16 at 18:41
  • 1
    Casting to a string should get you the query string, e.g. (string) $query – Clive Jul 25 '16 at 19:10
  • ugh. So I'm getting a this when I try to execute this statement: Fatal error: Call to undefined method Drupal\views\Plugin\views\query\Sql::leftJoin() This is the snippet of code I'm trying to implement $query->leftJoin('book', 'b', 'node.nid = b.nid'); $query->addWhere(0,'book,article','IN'); $query->addWhere(0,'b.has_children',1,'<>'); $query->addWhere(1,'b.nid','IS NULL'); – StevenPatz Jul 27 '16 at 17:33

Your Answer

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

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