0

It seems kinda trivial but I can find out an example on the Internet to do that kind of query:

$entityQuery = new EntityFieldQuery();
$query = $entityQuery->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', 'contact');
$query->propertyCondition('status', 1);
if (!empty($params_categories))
  $query->fieldCondition('field_category', 'tid', $params_categories, 'IN');
if (!empty($params_contextes))
  $query->fieldCondition('field_family', 'tid', $params_contextes, 'IN');
if (!empty($params_organismes))
  $query->fieldCondition('field_organisme', 'tid', $params_organismes, 'IN');
if (!empty($params_fonctions))
  $query->fieldCondition('field_function', 'tid', $params_fonctions, 'IN');
$query->count = TRUE;
$count = $query->execute();

Where $params_xxx are arrays of tid.

I need to execute these fieldConditions with an "OR" operator. That means that I want to retrieve each node that has at least one tid among the four taxonomy fields.

Now, it seems to act like an "AND" operator and sends me only those that have one tid for each taxonomy field.

Any clue is welcome.

The less complicated solution I eventually implemented:

I decided to proceed by mini queries that I merge together and erase duplicates.

/**
* Implements hook_form_submit().
*/
function grap_contact_repository_form_submit($form, &$form_state) {
  $matches = array();
  $query   = array();
  $categories = $form_state['values']['categories'];
  if (!empty($categories)) {
    grap_contact_get_mini_query('field_category', $categories, $matches);
    $query['categories'] = array_values($categories);
  }
  $contextes  = $form_state['values']['contextes'];
  if (!empty($contextes)) {
    grap_contact_get_mini_query('field_family', $contextes, $matches);
    $query['contextes'] = array_values($contextes);
  }
  $organismes = grap_contact_get_tid_from_name($form_state['values']['organismes']);
  if ($organismes[0] != null) {
    grap_contact_get_mini_query('field_organisme', $organismes, $matches);
    $query['organismes'] = array_values($organismes);
  }
  $fonctions  = grap_contact_get_tid_from_name($form_state['values']['fonctions']);
  if ($fonctions[0] != null) {
    grap_contact_get_mini_query('field_function', $fonctions, $matches);
    $query['fonctions'] = array_values($fonctions);
  }
  // I have all my data in $matches
}

My mini query function:

function grap_contact_get_mini_query($field, $terms, &$matches) {
  $entityQuery = new EntityFieldQuery();
  $query = $entityQuery->entityCondition('entity_type', 'node');
  $query->entityCondition('bundle', 'contact');
  $query->propertyCondition('status', 1);
  $query->fieldCondition($field, 'tid', $terms, 'IN');
  $results = $query->execute();
  if(count($results)) {
    foreach ($results['node'] as $n) {
      // I use the nid as the array key to overwrite the duplicates more easily
      $matches[$n->nid] = $n->nid;
    }
  }
}
6
  • If there is something fundamentally different, that invalidates answers to linked question, edit your question and I will retract my "duplicate" vote. But I hope what I linked will solve your problem.
    – Mołot
    Sep 30, 2014 at 9:53
  • No there isn't. It looks like what I need. But it seems also way too tricky for me. If the EFQ class doesn't support OR conditions, isn't it more simple to use a db_query() instead of creating a new class extending EFQ etc, etc...?
    – Djouuuuh
    Sep 30, 2014 at 9:59
  • Please excuse my lack of knowledge, I'm still learning this huge tool...
    – Djouuuuh
    Sep 30, 2014 at 10:00
  • 1
    You don't need to extend @Djouuuuh, just use hook_query_alter() or hook_query_tag_alter(), and ->addTag(). You'll be able to alter the query as if it were a standard SelectQuery
    – Clive
    Sep 30, 2014 at 10:04
  • I don't think this is a duplicate....There are two scenarios, OR operator used for 2 possible values of one field, and OR operator between 2 different fields with the same value....What the questioner asked, was not answered in the linked or "duplicate" thread. The linked question asked about 2 values with one field, and not one value for either one of two fields. Jul 24, 2015 at 0:01

0

Browse other questions tagged or ask your own question.