You are here

public function WebformEntityStorage::getSerial in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformEntityStorage.php \Drupal\webform\WebformEntityStorage::getSerial()

Returns the next serial number for a webform's submission.

Return value

int The next serial number for a webform's submission.

Overrides WebformEntityStorageInterface::getSerial

File

src/WebformEntityStorage.php, line 239

Class

WebformEntityStorage
Storage controller class for "webform" configuration entities.

Namespace

Drupal\webform

Code

public function getSerial(WebformInterface $webform) {

  // Use a transaction with SELECT … FOR UPDATE to lock the row between
  // the SELECT and the UPDATE, ensuring that multiple Webform submissions
  // at the same time do not have duplicate numbers. FOR UPDATE must be inside
  // a transaction. The return value of db_transaction() must be assigned or
  // the transaction will commit immediately.
  //
  // The transaction will commit when $transaction goes out-of-scope.
  //
  // @see \Drupal\Core\Database\Transaction
  $transaction = $this->database
    ->startTransaction();

  // Get the next_serial value.
  $next_serial = $this->database
    ->select('webform', 'w')
    ->forUpdate()
    ->fields('w', [
    'next_serial',
  ])
    ->condition('webform_id', $webform
    ->id())
    ->execute()
    ->fetchField();

  // $next_serial must be greater than any existing serial number.
  $next_serial = max($next_serial, $this
    ->getMaxSerial($webform));

  // Increment the next_value.
  $this->database
    ->update('webform')
    ->fields([
    'next_serial' => $next_serial + 1,
  ])
    ->condition('webform_id', $webform
    ->id())
    ->execute();
  return $next_serial;
}