You are here

public function ResultSet::preLoadResultItems in Search API 8

Loads all "original objects" of the result items that have not been loaded.

This can be used in case original objects are needed for all results, to make sure they are multi-loaded, avoiding the performance penalty associated with loading them individually.

Overrides ResultSetInterface::preLoadResultItems

File

src/Query/ResultSet.php, line 113

Class

ResultSet
Represents the result set of a search query.

Namespace

Drupal\search_api\Query

Code

public function preLoadResultItems() {
  $item_ids = [];
  foreach ($this->resultItems as $item_id => $object) {
    try {
      if (!$object
        ->getOriginalObject(FALSE)) {
        $item_ids[] = $item_id;
      }
    } catch (SearchApiException $e) {

      // Can't actually be thrown here, but catch for the static analyzer's
      // sake.
    }
  }
  if (!$item_ids) {
    return;
  }
  $objects = $this
    ->getQuery()
    ->getIndex()
    ->loadItemsMultiple($item_ids);
  foreach ($objects as $item_id => $object) {
    $this->resultItems[$item_id]
      ->setOriginalObject($object);
  }
}