Two different ways to query entities / nodes: entityQuery and entityTypeManager

Submitted bybarnettech onTue, 03/09/2021 - 15:32
 $results = \Drupal::entityQuery('app_voucher')
      ->range(0, 20000)
      ->execute();
foreach ($results as $result) {
      $messenger = \Drupal::messenger();
      $voucher_entity = \Drupal::entityTypeManager()->getStorage("app_voucher")->load($result);
      $voucher_id = $voucher_entity->voucher_id->getValue();
      $product_id = $voucher_entity->product_id->getValue();

}

Here are 2 different ways to get the same sort of data:

$courseGroupClasses = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->loadByProperties(['type' => 'course_group_class', 'field_company' => $company2Object->id()]);

Or

 $voucherNodes = \Drupal::entityQuery("node")
        ->condition("type", "voucher")
        ->condition("field_company.target_id", $company2Object->id())
        ->execute();