The following fails with an exception.
\Drupal::entityTypeManager()->getStorage('foo_module_division')->loadByProperties(['enabled'=>TRUE, 'parent_code'=> NULL]).
How else do I specify I want to load entities whose parent_code field is NULL
?
The exception is the result of how EntityStorageBase::loadByProperties()
builds the underlying WHERE
clause; it's utilizing ::buildPropertyQuery()
which casts NULL
to an array then uses the IN
operator to compare; this won't work since you need a IS NULL
query to find NULL
entries in the DB.
Instead, you can build your own entity storage query in a similar way as loadByProperties()
:
$es = \Drupal::entityTypeManager()->getStorage('foo_module_division')
$q = $es->getQuery();
$results = $q->condition('parent_code', NULL, 'IS')->condition('enabled', TRUE)->execute();
return $results ? $es->loadMultiple($results) : array();
IS NULL
is now written like so; condition('parent_code', '', 'IS NULL')
Commented
Aug 2, 2023 at 7:54
The example of getting articles that has empty body:
$entityStorage = \Drupal::entityTypeManager()->getStorage('node');
$query = $entityStorage->getQuery();
$query->condition('type', 'article');
$query->notExists('body');
$nids = $query->execute();
$nodes = $entityStorage->loadMultiple($nids);