Today I faced a strange problem using EntityFieldQuery with OG.

I have a group A containing nodes of type B.
I want to retreive all nodes of type B into my group A and which have a given taxonomy term.

Here is the code I use :

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'B')
  ->fieldCondition('field_mytaxonomy', 'tid', $tids, 'IN')
  ->fieldCondition('og_group_ref', 'target_id', $gid)
  ->fieldOrderBy('field_mydate', 'value', 'ASC');
$result = $query->execute();

The problem is that I have a user in my group A which UID is the same that a node's NID of type B (using one of the $tids terms) which is NOT in the group A.
In this particular case, the EntityFieldQuery returns this node with the other ones.

It becomes funny when I use the following code, which does not address my needs, because in this case, the faulty node does not appear in the result anymore.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'B')
//  ->fieldCondition('field_mytaxonomy', 'tid', $tids, 'IN')
  ->fieldCondition('og_group_ref', 'target_id', $gid)
  ->fieldOrderBy('field_mydate', 'value', 'ASC');
$result = $query->execute();

I think something is wrong with the query rewrite made by OG but it is a bit too complicated for me to understand where.
If you need more details, feel free to ask. Good luck !

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

amitaibu’s picture

If you are able to reproduce this case in a (failing) simpleTest, we can take care of fixing it :)

DuaelFr’s picture

You know I am not good at tests ;)
I will try to find some help to make it !

DuaelFr’s picture

Status: Active » Needs review
FileSize
5.89 KB

I did not find any help so I hope my test is correct.
It took me time but I reproduced the issue on a clean install.

Tell me if something is wrong.

Status: Needs review » Needs work

The last submitted patch, og-add_test_to_demonstrate_efq_issue-2030787-3.patch, failed testing.

amitaibu’s picture

> You know I am not good at tests ;)

No one is born knowing simpletest ;) Awesome thanks, we'll go over it!

RoySegall’s picture

@DuaelFr can you supply a db dump of the clean installation that you managed to reproduce the problem? Your simple test are reproducing the problem but when i try to reproduce this i'm unable(even with a normal user).

DuaelFr’s picture

I have no access to my computer for now but I made this test on a clean install with the testing installation profile and only OG and its dependencies (+ taxonomy core module).
Say me if you really need this dump and i'll send it later.

RoySegall’s picture

On a clean installation the taxonomy module is enable automatically - no need to enable it in the simple test.

It's OK that you simple test are able to reproduce the problem but i can't debug the problem through the simple test therefor i'll need your db dump. You can make the dump as soon you'll be near your PC.

DuaelFr’s picture

FileSize
98.23 KB

The dump is attached.

Here is the code I put in devel/php to trigger the bug with this data :

$gid = 1;
$tids = array(1);

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'content')
  ->fieldCondition('field_myvocab', 'tid', $tids, 'IN')
  ->fieldCondition('og_group_ref', 'target_id', $gid);

$result = $query->execute();
dpm($result);

Node #1 and #2 are group1 and group2
Node #3 is in group1
Node #4 is in group2

Users #2 and #3 are useless
User #4 is in group2

Enjoy the debugging :)

DuaelFr’s picture

joachim’s picture

Title: Edge case with EntityFieldQuery » EntityFieldQuery with fieldCondition() on OG group audience field breaks if other fieldConditions come before the OG field
Priority: Normal » Major

I've finally managed to track this one down. It's definitely not an edge case and very easy to reproduce. Suppose I want all the nodes in a group that have a value of 'red' in another field:

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node');
  $query->entityCondition('bundle', 'test_og_efq');
  
  // OG group
  $query->fieldCondition('og_group_ref', 'target_id', $gid);
  // Field condition on other field.
  $query->fieldCondition('field_test_og_efq_field', 'value', 'red');

  $result = $query->execute();

This works!

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node');
  $query->entityCondition('bundle', 'test_og_efq');
  
  // Field condition on other field.
  $query->fieldCondition('field_test_og_efq_field', 'value', 'red');
  // OG group
  $query->fieldCondition('og_group_ref', 'target_id', $gid);

  $result = $query->execute();

This doesn't work! Nodes are returned that are NOT in the requested group.

The only thing I've changed is the order in which the fieldCondition()s are applied. If the fieldCondition() on the OG group audience field is not first, then the query breaks.

Working on a simpletest...

joachim’s picture

Title: EntityFieldQuery with fieldCondition() on OG group audience field breaks if other fieldConditions come before the OG field » EntityFieldQuery with fieldCondition() on OG group audience field breaks if other fieldConditions come before the OG field and there are multiple group content entityypes with

One more thing to get it to break: you need to have other entities that are group content.

This is because the join to og_membership doesn't limit on entity type. Hence what's returned is entity IDs that are possibly not nodes at all!

joachim’s picture

And another thing:

You need to have one of the entities that you actually want, that's getting retrieved by the query in the OTHER field, match up with an entity of another type in the og_membership table.

Dumping $query as a string at the end of og_query_og_membership_alter() and putting in the values to run it directly in the DB shows the problem.

joachim’s picture

Here's a test that fails, and a patch that fixes the problem.

I am not sure how legit it is to go reaching into the original EFQ for the entity type -- I've always used an EFQ with an entity type property, so I assume that's a requirement and will therefore always be there?

Status: Needs review » Needs work

The last submitted patch, 14: 2030787.og_.entity-field-query-field-conditions-order.patch, failed testing.

joachim’s picture

Huh. Will look into that tomorrow.

joachim’s picture

Status: Needs work » Needs review
FileSize
7.55 KB

Ah, I'd no idea that the entity_type entityCondition() can be multi-valued, or even not there at all!

Here's an updated patch with test + fix.

  • Commit 05f64fd on 7.x-2.x authored by joachim, committed by Amitaibu:
    Issue #2030787 by joachim, DuaelFr: EntityFieldQuery with fieldCondition...
amitaibu’s picture

Status: Needs review » Fixed

Wow, thanks guys -- not an easy one! Merged.

DuaelFr’s picture

Well done thank you very much joachim !

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.