9

I am on a custom entity with custom fields and i try to get all empty record on date field :

Field was create with CCK, so the field storage has a table.

  $query = \Drupal::entityQuery('test_entity');
        $query->condition('field_date',NULL,'=');
        $ids=$query->execute();

This doesn't work for date field.

5
  • Have you looked in the DB and confirmed that the values for those columns are actually null and not something else? The column is text or integer, depending on which field type you selected, so as far as a database query goes, there's nothing special about comparing it to null – Clive Oct 13 '16 at 12:43
  • it's a field create with the drupal interface, so the field is in fact a table, she has no record – Kevin Oct 13 '16 at 12:44
  • That'll be it then - the query will do an INNER JOIN on the field data table because you've included it in the conditions, and there's no data, so nothing to compare to null. Hence no results. – Clive Oct 13 '16 at 12:46
  • 1
    yes, so i replace with $query->notExists('field_date'); that seems to work, i ll try more test – Kevin Oct 13 '16 at 12:51
  • Oh brilliant, they accounted for that situation this time round, good news. Please do put an answer on once you've confirmed that works, this will be useful to a lot of people in time – Clive Oct 13 '16 at 12:53
18

Since the field is stored in a single table, this is how i get all empty record from my entity where my custom field is empty :

 $query = \Drupal::entityQuery('test_entity');
        $query->notExists('field_date');
        $ids=$query->execute();
5
  • What about when it's in multiple tables? – oknate Oct 13 '16 at 14:41
  • you need to add more conditions, like id or another field for filter. But your first filter is your entity ( entityQuery('test_entity') – Kevin Oct 13 '16 at 14:45
  • This is great for when you add a new field to a content type but you can't bulk save them all for some reason. – Lester Peabody Apr 18 '18 at 18:19
  • This is not working on a few fields. Any idea why it will behave like this? $appeal_query = \Drupal::entityQuery('node')->condition('status', 1)->condition('type', 'appeal')->notExists('field_appeal_country'); – Suraj May 22 '20 at 2:42
  • Just for the record, here's the documentation for exists/notExists : api.drupal.org/api/drupal/… – JFK Jun 30 '20 at 4:27

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.