2
$query = \Drupal::entityQuery('node');

$group = $query->orConditionGroup()
 ->condition('field_ref1_user.entity:user.uid', $user->id()) 
 ->condition('field_ref2_user.entity:user.uid', $user->id());

$ids = $query
 ->condition('status', 1)
 ->condition('type', 'mynodetype')
 ->condition($group)         
 ->execute();

The first condition in $group met 3 nodes and the second condition met 1 additional node (different from 3 nodes of condition 1) so since it is OR condition I expect to get 4 node IDs but I receive only 3 node IDs that met the first $group condition.

If I switch the conditions within the $group, I get 1 node ID, that met the first condition. It's seems that the second condition in the group is ignored.

What is wrong with my code? Is there a way to see the query that actually executed?

Edit I have activated WebProfiler to see the actual query.

WHERE (node_field_data.status = '1') AND (node_field_data.type = 'mynodetype') AND ((users_field_data.uid = '11') or (users_field_data.uid = '11'))

These are 2 different user reference fields but for both of them the query checks users_field_data.uid = '11'

4
  • I can't see why you're getting "and" instead of "or", but if the field_ref1_user and field_ref2_user fields are user id fields, maybe try changing the conditions to just: condition('field_ref1_user', $user->id()) and condition('field_ref2_user', $user->id())
    – user72086
    Jun 5, 2017 at 13:34
  • Looks to me like there might be some sort of problem chaining from the entity to the fields. I'd expect two different aliases for the user table in your query, but they're both the same. Jun 5, 2017 at 15:52
  • I thought this would be a bug after a first look, turns out not: drupal.stackexchange.com/questions/185541/…
    – Clive
    Jun 5, 2017 at 18:06
  • Lisa, it is "OR" condition group. It was my mistake. @Clive, user can be referenced in the node as ref1 or ref2. I want to get the list of nodes where the user referenced as ref1 OR ref2. It is a legitimate request and I thought the code I provided will give the expected result. You not mentioned the problem in my code but giving a link to post that claims this is how drupal should work. What should be a code to get the result I expect?
    – Igal
    Jun 5, 2017 at 19:08

2 Answers 2

0

I can't see any issues with your code, and this might be a dumb answer, but a while back I was having trouble with chaining together entity query conditions. I got everything working by splitting it into individual statements. It didn't seem logical that that should make a difference, but it did. (Though, I'm sure it's possible I had some other logical problem that was the actual issue, and separating into statements was just incidental.) But maybe try...

$group = $query->orConditionGroup();
$group->condition('field_ref1_user.entity:user.uid', $user->id());
$group->condition('field_ref2_user.entity:user.uid', $user->id());

$query->condition('status', 1);
$query->condition('type', 'mynodetype');
$query->condition($group);         
$ids = $query->execute();
3
  • I get the same results with you code. Is there a way to see the query that actually executed?
    – Igal
    Jun 5, 2017 at 2:56
  • 1
    Sorry that didn't help. I went looking for info on debugging entity queries and learned something new: if you have devel enabled, you can add: $query->addTag('debug') to see the query. Hope that helps!
    – user72086
    Jun 5, 2017 at 12:34
  • Thanks. I activated WebProfiler to see the query. I have updated the first post with the query.
    – Igal
    Jun 5, 2017 at 12:58
0

Meantime, until I get more elegant solution from community here, I will go for the following ugly but working solution

$query1 = \Drupal::entityQuery('node');
$ids1 = $query1
 ->condition('field_ref1_user.entity:user.uid', $user->id())
 ->condition('status', 1)
 ->condition('type', 'mynodetype')
 ->execute();

$query2 = \Drupal::entityQuery('node');
$ids2 = $query2
 ->condition('field_ref2_user.entity:user.uid', $user->id())
 ->condition('status', 1)
 ->condition('type', 'mynodetype')     
 ->execute();

$ids1 and $ids2 arrays to be merged into $ids and then I will sort the $ids and remove duplicates

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.