0

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?

3
  • To my knowledge neither Drupal 7 or Drupal 8 entity field queries support Joins out of the box.
    – tenken
    Commented Dec 11, 2015 at 23:03
  • @tenken thanks. So if I want a Join I have to use db_query ? Im not expert of d7
    – monkeyUser
    Commented Dec 11, 2015 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
    Commented Dec 11, 2015 at 23:09

1 Answer 1

1

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')
  ->execute();

$users = User::loadMultiple($ids);
4
  • Active / blocked for users still use literals and not constants in Drupal 8?
    – mpdonadio
    Commented Dec 12, 2015 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
    Commented Dec 13, 2015 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
    Commented Dec 13, 2015 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
    Commented Dec 13, 2015 at 17:36

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.