I have the following query, which works.

  $email = db_query('
    Select u.init as email 
    FROM user__roles as r 
    JOIN users_field_data as u ON r.entity_id = u.uid 
    WHERE u.status = 1 
      AND r.roles_target_id = :role
  ', array('role' => 'pubblisher'))->fetchAll();

I want to convert it to use EntityFieldQuery, but I didn't find any Join function in the documentation. How do I convert my code to use EntityFieldQuery?

  • To my knowledge neither Drupal 7 or Drupal 8 entity field queries support Joins out of the box. – tenken Dec 11 '15 at 23:03
  • @tenken thanks. So if I want a Join I have to use db_query ? Im not expert of d7 – monkeyUser Dec 11 '15 at 23:07
  • hmmm maybe not, while there is no Join() function you may be able to achieve it using sub-entities. See: drupal.org/node/1882418 – tenken Dec 11 '15 at 23:09

As long as you only query for fields, entity query does the joins automatically for you.

Note that entity query can only return entity ID's, you then need to load them.

$ids = \Drupal::entityQuery('user')
  ->condition('status', 1)
  ->condition('roles', 'pubblisher')

$users = User::loadMultiple($ids);
  • Active / blocked for users still use literals and not constants in Drupal 8? – mpdonadio Dec 12 '15 at 21:56
  • Apparently, I couldn't find a constant for it. If you have a user entity, then you can use isActive()/isBlocked(). – Berdir Dec 13 '15 at 8:02
  • nice, What is the difference between EntityFieldQuery and entityQuery ? When I Want to retrieve only field of the same entity i use EFQ instead when I have to create join I have to use entityQuery ? Thanks – monkeyUser Dec 13 '15 at 17:13
  • EntityFieldQuery is how it is called in D7, it doesn't exist in D8. It's the same thing, more or less, just a different name. – Berdir Dec 13 '15 at 17:36

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.