You are here

public function NewResultsCheck::saveKnownResults in Search API Saved Searches 8

Saves the known ("old") results for a saved search.

Parameters

\Drupal\search_api_saved_searches\SavedSearchInterface $search: The saved search.

\Drupal\search_api\Item\ItemInterface[] $items: The known results to save. Passing already saved results here will cause this method to fail.

Return value

bool TRUE if the operation succeeded, FALSE otherwise.

1 call to NewResultsCheck::saveKnownResults()
NewResultsCheck::getNewResults in src/Service/NewResultsCheck.php
Retrieves new results for the given search.

File

src/Service/NewResultsCheck.php, line 306

Class

NewResultsCheck
Provides a service for checking saved searches for new results.

Namespace

Drupal\search_api_saved_searches\Service

Code

public function saveKnownResults(SavedSearchInterface $search, array $items) {
  $insert = Database::getConnection()
    ->insert('search_api_saved_searches_old_results')
    ->fields([
    'search_id',
    'search_type',
    'item_id',
  ]);
  $search_id = $search
    ->id();
  $type_id = $search
    ->bundle();
  foreach (array_keys($items) as $id) {
    $insert
      ->values([
      'search_id' => $search_id,
      'search_type' => $type_id,
      'item_id' => $id,
    ]);
  }
  try {
    $insert
      ->execute();
    return TRUE;
  } catch (\Exception $e) {
    $vars['@search_id'] = $search
      ->id();
    $vars['%search_label'] = $search
      ->label();
    $this
      ->logException($e, '%type while trying to save known results for saved search #@search_id (%search_label): @message in %function (line %line of %file).', $vars);
    return FALSE;
  }
}