0

I'm building an entity query and I would like to check if one field contains one string or another. I'm building the query like so.

$searchname = 'test'; 
$searchtype' = 'type_1';

$query->condition('status', NODE_PUBLISHED);
$query->condition('field_type', $searchtype); 
$query->sort('created','DESC');

However I'd like to check if field_type is either $searchtype or 'both' I attempted to do so using an orConditionGroup like so

$orCondition = $query->orConditionGroup();
$query->condition('status', NODE_PUBLISHED);
$orCondition->condition('status', NODE_PUBLISHED);

$query->condition('field_type', $searchtype); 
$orCondition->condition('field_type','both');
$query->condition($orCondition);
$query->sort('created','DESC');
$nids = $query->execute();

However it acts like the orCondition isn't being considered at all. Using $orCondition it still does not produce nodes that have 'field_type' = 'both'. How would you go about checking whether a field contains one string or another in an entity query?

UPDATE: I added the field name to the or condition because I figured I would need to add it to both if I want it checked in both conditions. If there's a way around that, that'd be even better.

UPDATE 2: I made a second attempt using this code but I receive a 504 Timeout error when loading the page

$searchtype = 'test';
$query        = \Drupal::entityQuery('node');
$conditionOne = $query->orConditionGroup();
$conditionTwo = $query->orConditionGroup();

$conditionOne->condition('status', NODE_PUBLISHED);
$conditionTwo->condition('status', NODE_PUBLISHED);

$conditionOne->condition('field_type', $searchtype); 
$conditionTwo->condition('field_type','both');

$query->condition($conditionOne);
$query->condition($conditionTwo);
$query->sort('created', 'DESC');
$nids = $query->execute();

What I'm ultimately trying to accomplish is this logic

(status = true) AND (field_type = 'foo' or 'both')

  • The code doesn't seem to match up with I'd like to check if field_type is either $searchtype or 'both' . The or group has field_name and field_type in it, not two field_type conditions – Clive Mar 22 '17 at 0:28
  • I just put in field_name in the or group because I was under the impression that I would have to a repeat of the previous query with just a different field_type. Like the same logic as: 'if(field_name =='name' && field_type=='type1' || field_name=='name' && field_type=='both' ) – Matt Mar 22 '17 at 2:56
  • You've lost me, your updated code now has "status" in it, which your last comment doesn't. I think you need to nail down what you want to accomplish first, it seems a bit confused at the moment. – Clive Mar 22 '17 at 16:05
  • I'm just putting in a random field. The only thing I care about is getting an orConditionGroup to work, the specifics of the other fields don't matter to me, they're just example placeholders. You could replace status with any other random field and my question is the same. I'll update my question for clarity. – Matt Mar 22 '17 at 16:13
  • Without knowing the goal it's hard to point out where the problem is. e.g. your latest code would yield where (status = 1 OR field_type = 'foo') AND (status = 1 OR field_type = 'both'). Is that right or wrong? – Clive Mar 22 '17 at 16:17
2

So, the simplest way to rewrite this, would be:

$searchname = 'test'; 
$searchtype = 'type_1';

$field_types_array = [$searchtype, 'both'];
$query->condition('status', NODE_PUBLISHED);
$query->condition('field_type', $field_types_array, 'IN');
$query->sort('created','DESC');

The use of an array for the condition method forces the SQL query to be written as an IN query.

Reference documentation: QueryInterface::condition

  • When attempting to use $query->condition('field_type', $field_types_array); by itself I receive an error 'Invalid parameter number: number of bound variables does not match number of tokens', but when using IN it worked. – Matt Mar 22 '17 at 16:49
  • Surprising, I need to check that part of my knowledge again, Thought the same as @Clive did, IN is implied when the second argument is an array. Probably D7 leftovers in my head that don't apply to D8 – Stefanos Petrakis Mar 22 '17 at 17:13
  • @Matt Tested and I get the same error you mentioned, I was under the false impression, thanks for the tip. I updated the answer as well. – Stefanos Petrakis Mar 22 '17 at 17:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.