Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'd like to run a query using the EntityFieldQuery object. I need value from both the node and node_access table therefore I'd need to use INNER JOIN. From the d.o documentation I can't figure out how this is possible.

Here's what I have --

$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'node_access')
->propertyCondition('type', 'external_link')
->propertyCondition('status', 1)
->fieldCondition('gid', '3', '=')
->fieldCondition('realm', 'domain_id', '=')
->fieldCondition('grant_view', '1', '>=')
->range(0,1)
->execute();
share|improve this question
add comment

2 Answers

You can't add extra joins to an EntityFieldQuery directly (it's not supported), but you could add a tag to the query, implement hook_query_TAG_alter(), and add the join manually when the query is converted down to a standard db query.

This isn't tested but will probably get you most of the way there:

$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'node_access')
  // etc
  ->addTag('MYTAG');

// get the query results as normal

And then the query alter function:

function MYDMOULE_query_MYTAG_alter(QueryAlterableInterface $query) {
  $query->join('node_access', 'node_access', 'node_access.nid = node.nid');
}

The other way to do it would be to subclass EntityFieldQuery itself and add the join, but I think the above method is simpler in this case.

share|improve this answer
    
Is there a better way to do it in a single function? Even if it's not EntityFieldQuery –  allanb Oct 5 '12 at 17:16
1  
Not really, the only other way is to build the query up manually using db_select, then you can have as much control as you want over it –  Clive Oct 5 '12 at 17:21
    
I'll go with that. Thx –  allanb Oct 5 '12 at 17:33
    
@Clive ... oh this answer is really interesting. Like it. :P –  tenken Oct 5 '12 at 17:51
1  
@Michiel No EntityFieldQuery doesn't do any caching, it just wraps around a SelectQuery and adds some methods for entities. Those extra methods account for the slight (very slight) performance decrease you'd experience using it as opposed to a regular SelectQuery –  Clive Jun 28 '13 at 11:41
show 6 more comments

Although it can't be done on-the-fly in Drupal 7, it can be done in Drupal 8 (unreleased at the time of this writing).

See Entity Field query got join support for details (including an example).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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