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?

1 Answer 1

5

The method is actually named leftJoin:

$query->leftJoin('book', 'b', 'n.nid = b.nid');
3
  • 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
    Commented Jul 25, 2016 at 18:41
  • 1
    Casting to a string should get you the query string, e.g. (string) $query
    – Clive
    Commented Jul 25, 2016 at 19:10
  • 1
    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
    Commented Jul 27, 2016 at 17:33

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.