I have a custom content type user_in_club
and I'd like to load a single specific node matching two field values. There are 2 ways (I know of) which can solve this:
$result = \Drupal::entityQuery('node')
->condition('type', 'user_in_club')
->condition('field_user_id', $account->id())
->condition('field_club_id',$active_club)
->execute();
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($result);
OR
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties(['type' => 'user_in_club', 'field_user_id' => $account->id(), 'field_club_id' => $active_club ]);
In my single example only one node id is returned, but in case I have multiple node ids returned, is one method to be preferred over the other in terms of performance etc ? I currently don't see a big difference other than ETQ is better to create more complex queries.
entityQuery
you can also limit your result set via$query->range(0, 100)
as an example. I don't believe you can do that withloadByProperties
- correct me if I am wrong. So for smaller result setsloadByProperties
might be more practical but as you suggest for more complex or performance-critical queries on large data-sets,entityQuery
might be the way to go. Also you could callentityTypeManager()->getStorage('node')->getQuery
which effectively does the same thing by exposing the more powerful query interface.