1

My custom entity type comes with a string field for which I would prefer case sensitive queries. When I use

$query = \Drupal::entityQuery('my_entity_type')
 ->condition('my_token_field', 'SomeSortofTokenString')
 ->execute();

the result will include all entities no matter if the string in my_token_field is written the same way or for example with lowercases only. my_token_field is set a base field of type string - maybe this is the problem?

2

2 Answers 2

1

As the issue in a comment under my question describes this is the way to go:

 $given_token = 'AbTg4576Ft6h6';
 $result = \Drupal::database()->select('my_database_table', 't')
    ->where(
      'binary my_token_field = :given_token',
      ['given_token' => $given_token]
    )
    ->fields('t', ['id'])
    ->execute()
    ->fetchAll();
1

Like you already said, I think you should set case_sensitive to true in the field settings, like here. This can also be set in the config.

When digging in the code, I found that the condition function at some point leads to translateCondition which checks if a field is case sensitive and automatically applies the binary option to the sql query.

1
  • This does not help as the question is about a base field in a custom entity, not a Field API field Commented Nov 12, 2020 at 16:00

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.