0

I'm currently performing an entity query like so.

$search = 'test';
$type = 'page';
$query = \Drupal::entityQuery('node')
    ->condition('type',$type);

I'm then adding an 'or' condition like so

$or_condition = $query->orConditionGroup()
    ->condition('title', '%'.$search.'%', 'like')
    ->condition('body', '%'.$search.'%', 'like');
$query->condition($or_condition);
$query->execute();

This works with no issue, but I would like to be able to continue to add conditions to the $or_condition. Here is my attempt.

$search[0] = 'test';
$search[1] = 'test2';

$query = \Drupal::entityQuery('node')
    ->condition('type',$type);

$or_condition = $query->orConditionGroup()
    ->condition('title', '%'.$search[0].'%', 'like')
    ->condition('body', '%'.$search[0].'%', 'like');
$or_condition->condition('title', '%'.$search[1].'%', 'like')
    ->condition('body', '%'.$search[1].'%', 'like');
$query->condition($or_condition);
$query->execute();

However the code above does not work. The ultimate goal is to be able to explode a user provided string and include it into the or condition so the word order of what the user searches doesn't matter. Is there a way to append conditions to an or condition group outside of the declaration statement?

  • this should work, but like is not a supported condition by entity query, it has CONTAINS, ENDS_WITH, STARTS_WITH. Don't use %. Also, sounds like you might want to use search_api instead as adding a lot of conditions like this is going to be pretty inefficient. – Berdir Feb 7 '17 at 17:26
  • Try putting each set in a separate OR condition, and group both sets in a larger OR condition. It sounds useless, but it might work. – Eyal Feb 7 '17 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.