[development] Drupal7 EntityFieldQuery Assistance

Greenman greenman at the-organization.com
Fri Jan 14 15:47:49 UTC 2011

Hi Devs

I would like some more eyes on my query. It is not producing the SQL I

Working with OG for D7, I am looking to select all the group users with a
specific state. I would have expected this code to do what I wanted, but, it
does not.

I have attached the code and sql below. The SQL is the result of running the
query on gid = 2 and states = 1. When run, I am getting results for both
states 1 and 3.

Anyone got any more insight ?

Thanks very much


  $query = new EntityFieldQuery;
    ->entityCondition('entity_type', 'user')
    ->fieldCondition('group_audience', 'gid', $gid)
    ->fieldCondition('group_audience', 'state', (array) $states, 'IN');

SELECT DISTINCT field_data_group_audience0.entity_type AS entity_type,
field_data_group_audience0.entity_id AS entity_id,
field_data_group_audience0.revision_id AS revision_id,
field_data_group_audience0.bundle AS bundle
FROM field_data_group_audience field_data_group_audience0
INNER JOIN field_data_group_audience field_data_group_audience1 ON
field_data_group_audience1.entity_type =
AND field_data_group_audience1.entity_id =
( field_data_group_audience0.group_audience_gid = '2' ) AND
(field_data_group_audience1.group_audience_state IN ('1'))
AND ( field_data_group_audience0.deleted = '0' ) AND (
field_data_group_audience0.entity_type = 'user' ) LIMIT 0 , 30

The output (slightly modified to add state), gives me this:

 entity_type state entity_id revision_id bundle   user 1 1 1 user  user 3 2
2 user

Not sure if the formatting will hold, but, due to the joins, I am getting
both states 1 and 3.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20110114/c7f37493/attachment.html 

More information about the development mailing list