You are here

public function DatabaseQueue::createItemMultiple in Purge 8.3

Add multiple items to the queue and store them efficiently.

Parameters

array $items: Non-associative array containing arrays with arbitrary data to be associated with the new tasks in the queue.

Return value

array|false Non-associative array containing unique ID's for the items that were saved successfully, otherwise FALSE. We don't guarantee the item was committed to disk etc, but as far as we know, the item is now in the queue.

Overrides QueueInterface::createItemMultiple

File

src/Plugin/Purge/Queue/DatabaseQueue.php, line 69

Class

DatabaseQueue
A QueueInterface compliant database backed queue.

Namespace

Drupal\purge\Plugin\Purge\Queue

Code

public function createItemMultiple(array $items) {
  $item_ids = $records = [];

  // Build a array with all exactly records as they should turn into rows.
  $time = time();
  foreach ($items as $data) {
    $records[] = [
      'data' => serialize($data),
      'created' => $time,
    ];
  }

  // Insert all of them using just one multi-row query.
  $query = $this->connection
    ->insert(static::TABLE_NAME, [])
    ->fields([
    'data',
    'created',
  ]);
  foreach ($records as $record) {
    $query
      ->values($record);
  }

  // Execute the query and finish the call.
  if ($id = $query
    ->execute()) {
    $id = (int) $id;

    // A multiple row-insert doesn't give back all the individual IDs, so
    // calculate them back by applying subtraction.
    for ($i = 1; $i <= count($records); $i++) {
      $item_ids[] = $id;
      $id++;
    }
    return $item_ids;
  }
  else {
    return FALSE;
  }
}