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 !
Comments
Comment #1
amitaibuIf you are able to reproduce this case in a (failing) simpleTest, we can take care of fixing it :)
Comment #2
DuaelFrYou know I am not good at tests ;)
I will try to find some help to make it !
Comment #3
DuaelFrI 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.
Comment #5
amitaibu> You know I am not good at tests ;)
No one is born knowing simpletest ;) Awesome thanks, we'll go over it!
Comment #6
RoySegall CreditAttribution: RoySegall commented@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).
Comment #7
DuaelFrI 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.
Comment #8
RoySegall CreditAttribution: RoySegall commentedOn 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.
Comment #9
DuaelFrThe dump is attached.
Here is the code I put in devel/php to trigger the bug with this data :
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 :)
Comment #10
DuaelFrTake care of the changes introduced in #2039903: Cannot make an EntityFieldQuery using two OG fields as condition
Comment #11
joachim CreditAttribution: joachim commentedI'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:
This works!
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...
Comment #12
joachim CreditAttribution: joachim commentedOne 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!
Comment #13
joachim CreditAttribution: joachim commentedAnd 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.
Comment #14
joachim CreditAttribution: joachim commentedHere'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?
Comment #17
joachim CreditAttribution: joachim commentedHuh. Will look into that tomorrow.
Comment #18
joachim CreditAttribution: joachim commentedAh, 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.
Comment #20
amitaibuWow, thanks guys -- not an easy one! Merged.
Comment #21
DuaelFrWell done thank you very much joachim !